在main()中记录Throwable

时间:2012-03-30 10:12:56

标签: java throwable

由于不同帖子中列出的原因,抓取Throwable是不可取的。但是,有一个像下面这样的主要结构是否有意义?如果删除了Throwable行,则不会记录错误。

public static void main(String[] args) {
    try {
        launchMyApplication();
    } catch (SomeCheckedException e) {
        //recover if you can, log it if you can't
    } catch (Exception e) {
        //recover if you can (unlikely), log it if you can't
    } catch (Throwable e) {
        //Don't try to recover, but log it
        logger.error("Oops: {}", e);
    }
}

1 个答案:

答案 0 :(得分:10)

实现这种方式只会处理主线程上抛出的throwable。

解决此问题的最佳方法是使用Thread.setDefaultUncaughtExceptionHandler()