Java: Global Exception Handler

前端 未结 6 1396
孤独总比滥情好
孤独总比滥情好 2020-11-27 03:06

Is there a way to make a global exception-handler in Java. I want to use like this:

\"When an exception is thrown somewhere in the WHOLE program, exit.\"
         


        
6条回答
  •  Happy的楠姐
    2020-11-27 04:05

    Threads.setDefaultUncaughtExceptionHandler() works but not in all cases. For example, I'm using it in my main() before creating Swing widgets, and it works in the threads created by Swing, such as the AWT event thread or SwingWorker threads.

    Sadly, it doesn't have any effect on the thread created by javax.naming.spi.NamingManager.getInitialContext() when using an LDAP URL, using JavaSE 1.6. No doubt there are other exceptions.

提交回复
热议问题