From a45d1e0d079665088afe89a5b9c1d54a6a8203a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Hohwiller?= Date: Tue, 10 Mar 2020 09:28:02 +0100 Subject: [PATCH] Support for Thread.UncaughtExceptionHandler (#483) --- .../TDefaultUncaughtExceptionHandler.java | 29 +++++++++++++++++++ .../org/teavm/classlib/java/lang/TThread.java | 27 +++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 classlib/src/main/java/org/teavm/classlib/java/lang/TDefaultUncaughtExceptionHandler.java diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TDefaultUncaughtExceptionHandler.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TDefaultUncaughtExceptionHandler.java new file mode 100644 index 000000000..e0e2d7c7f --- /dev/null +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TDefaultUncaughtExceptionHandler.java @@ -0,0 +1,29 @@ +/* + * Copyright 2020 by Joerg Hohwiller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.classlib.java.lang; + +import org.teavm.classlib.java.lang.TThread.UncaughtExceptionHandler; + +public class TDefaultUncaughtExceptionHandler implements UncaughtExceptionHandler { + + @Override + public void uncaughtException(TThread t, Throwable e) { + + // print to browser console by default + e.printStackTrace(); + } + +} diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TThread.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TThread.java index 25a9f6222..3a9545f56 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TThread.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TThread.java @@ -28,6 +28,8 @@ public class TThread extends TObject implements TRunnable { private static TThread currentThread = mainThread; private static int nextId = 1; private static int activeCount = 1; + private static UncaughtExceptionHandler defaultUncaughtExceptionHandler = new TDefaultUncaughtExceptionHandler(); + private UncaughtExceptionHandler uncaughtExceptionHandler; private long id; private int priority; private boolean daemon; @@ -76,6 +78,8 @@ public class TThread extends TObject implements TRunnable { activeCount++; setCurrentThread(TThread.this); TThread.this.run(); + } catch (Throwable t) { + getUncaughtExceptionHandler().uncaughtException(this, t); } finally { synchronized (finishedLock) { finishedLock.notifyAll(); @@ -269,4 +273,27 @@ public class TThread extends TObject implements TRunnable { public TClassLoader getContextClassLoader() { return TClassLoader.getSystemClassLoader(); } + + public UncaughtExceptionHandler getUncaughtExceptionHandler() { + if (this.uncaughtExceptionHandler != null) { + return this.uncaughtExceptionHandler; + } + return defaultUncaughtExceptionHandler; + } + + public void setUncaughtExceptionHandler(UncaughtExceptionHandler uncaughtExceptionHandler) { + this.uncaughtExceptionHandler = uncaughtExceptionHandler; + } + + public static UncaughtExceptionHandler getDefaultUncaughtExceptionHandler() { + return defaultUncaughtExceptionHandler; + } + + public static void setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler handler) { + defaultUncaughtExceptionHandler = handler; + } + + public interface UncaughtExceptionHandler { + void uncaughtException(TThread t, Throwable e); + } }