Global Exception Handlers in Java

China☆狼群 提交于 2019-12-21 02:24:10

问题


I am thinking of setting up a global, default Exception handler for my (Android) Mobile application(which uses Java syntax) using Thread.setDefaultUncaughtExceptionHandler(...) call. I am thinking of just displaying an Alert Dialog with appropriate message to the user.

Are there any gotchas, caveats and rules that one needs to follow when setting DefaultExceptionHandlers? Any best practices like making sure that the process is killed, full stack trace is written to logs etc. ?

Links to documentation, tutorials etc. that can throw some light on this are welcome.

Thanks.


回答1:


  • The name is a bit misleading, because using that method will set a default exception handler for all threads.
  • Make sure no exceptions can be thrown from your exception handler.
  • If you're doing GUI stuff from your exception handler, make sure you're doing it from the right thread.
  • An uncaught exception will only stop the thread where the exception took place, if that also causes the process to terminate depends on any other threads that might be running.



回答2:


One possible problem with creating a global exception handler is you may get stuck in a loop of exceptions - some exceptions will be thrown repeatedly unless a program is terminated.

If you mean that when an exception is thrown, a message is displayed then the app is terminated, then you'll be fine.



来源:https://stackoverflow.com/questions/2815938/global-exception-handlers-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!