问题
I an an telephonic interview of java the interviewer asked few questions regarding java exceptions in which one of the question he asked is How can you handle unchecked exceptions globally in project.? now for this even i have to think for a moment by defining the custom error handler in java but this was not the correct answer , can you please advise how we can handle the unchecked exceptions globally in project..!
回答1:
I am not a mind reader, but the interviewer was probably asking about exception handlers:
- You can set a global handler by calling the static method Thread.setUncaughtExceptionHandler, or
- You can set a thread-specific handler by calling setUncaughtExceptionHandler on the individual thread, or
- If you are in a thread group, you can override ThreadGroup.uncaughtException() method (
ThreadGroup
implements the UncaughtExceptionHandler interface).
回答2:
You can set UnCaughtExceptionHandler
per Thread
probably your interview was looking it to handle for main Thread
Thread.setUnCaughtExceptionHandler()
来源:https://stackoverflow.com/questions/25696642/handling-unchecked-exceptions-globally-in-project