3

Catching Throwable is unadvisable for reasons outlined in different posts. However, would it make sense to have a main structured like below? If the Throwable line is removed, then errors would not be logged.

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);
    }
}
assylias
  • 310,138
  • 72
  • 642
  • 762

1 Answers1

10

Implementing this way will only handle throwables thrown on the main thread.

The best way to solve this issue is to use Thread.setDefaultUncaughtExceptionHandler().

assylias
  • 310,138
  • 72
  • 642
  • 762
ControlAltDel
  • 32,042
  • 9
  • 48
  • 75