Question

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);
    }
}
Was it helpful?

Solution

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top