How can I detect when an Exception's been thrown globally in Java?

后端 未结 10 475
自闭症患者
自闭症患者 2020-11-30 02:30

How can I detect when an Exception has been thrown anywhere in my application?

I\'m try to auto-magically send myself an email whenever an exception is thrown anywhe

10条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-30 03:07

    If you're using a web framework such as Spring then you can delegate in your web.xml to a page and then use the controller to send the email. For example:

    In web.xml:

    
      500
      /error/500.htm
    
    

    Then define /error/500.htm as a controller. You can access the exception from the parameter javax.servlet.error.exception:

    Exception exception = (Exception) request.getAttribute("javax.servlet.error.exception");
    

    If you're just running a regular Java program, then I would imagine you're stuck with public static void main(String[] args) { try { ... } catch (Exception e) {} }

提交回复
热议问题