【发布时间】:2013-01-10 09:49:54
【问题描述】:
我有一个全局异常处理程序例程,它将一些异常包装在运行时异常中
这样
public class ExceptionHandler
{
public static void handle(){
throw new RuntimeException(e);
}
}
在ExceptionHandler 类中,我还有一个静态构造函数
static
{
Thread.UncaughtExceptionHandler h = new Thread.UncaughtExceptionHandler()
{
@Override
public void uncaughtException(Thread thread, Throwable throwable)
{
Throwable t;
t = throwable;
if (throwable.getCause() != null)
t = throwable.getCause();
Log.e(t.getClass().getName(), t.getMessage(), t);
}
};
Thread.currentThread().setUncaughtExceptionHandler(h);
Thread.setDefaultUncaughtExceptionHandler(h);
}
问题是,在抛出 RTE 后,它不会进入 UncaughtExceptionHandler。为什么?
顺便说一句,我不能把它放到 main 方法中,因为我的 Android 程序中没有 main。
【问题讨论】:
-
见here
-
我认为静态块中的代码没有被执行,因为没有人使用 ExceptionHandler 类,所以没有启动 ExceptionHandler。 read this post。您可以扩展 android 的 Application 类并将此代码放在那里,Application 类的构造函数是“main”函数的某种模拟。
标签: java android exception runtimeexception uncaughtexceptionhandler