Is there an unhandled exception handler in Java?

前端 未结 5 1893
温柔的废话
温柔的废话 2020-12-03 08:00

If i remember correctly in .NET one can register \"global\" handlers for unhandled exceptions. I am wondering if there is something similar for Java.

5条回答
  •  渐次进展
    2020-12-03 08:14

    Assuming it is like catch(...) in C++ you would do:

    try
    {
       // your code here
    }
    catch(Throwable ex)
    {
       // any sort of exception, even if the VM has choked on a peanut
    }
    

    In general this isn't a good idea unless you are dealing with 3rd party code (you should try to always throw subclasses of Exception (and not RuntimeException) in your own code - unless it indicates a programmer error that should be delt with via unit testing.

提交回复
热议问题