简体   繁体   English

Java 异常 - 不使用 try catch 处理异常

[英]Java Exceptions - Handling exceptions without try catch

In Java, we handle exceptions using try catch blocks.在 Java 中,我们使用 try catch 块处理异常。 I know that I can write a try catch block like the one below to catch any exception thrown in a method.我知道我可以编写一个如下所示的 try catch 块来捕获方法中抛出的任何异常。

try {
  // do something
}
catch (Throwable t) {

}

But is there any way in Java which allows me to get a specific method called when an exception happens, instead of writing a catch-all method like the one above?但是在 Java 中有什么方法可以让我在发生异常时调用一个特定的方法,而不是像上面那样编写一个包罗万象的方法?

Specifically, I would like to show a user friendly message in my Swing application when an exception is thrown (which is not handled by my application logic).具体来说,当抛出异常(我的应用程序逻辑未处理)时,我想在我的 Swing 应用程序中显示一条用户友好的消息。

Thanks.谢谢。

By default, the JVM handles uncaught exceptions by printing the stack-trace to System.err stream.默认情况下,JVM 通过将堆栈跟踪打印到 System.err stream 来处理未捕获的异常。 Java allows us to customize this behavior by providing our own routine which implements Thread.UncaughtExceptionHandler interface. Java 允许我们通过提供我们自己的实现Thread.UncaughtExceptionHandler接口的例程来自定义此行为。

Take a look at this blog article which I wrote sometime back which explains this in detail ( http://blog.yohanliyanage.com/2010/09/know-the-jvm-1-uncaught-exception-handler/ ).看看我写的这篇博客文章,它详细解释了这一点( http://blog.yohanliyanage.com/2010/09/know-the-jvm-1-uncaught-exception-handler/ )。

In summary, all you have to do is write your custom logic as below:总之,您所要做的就是编写您的自定义逻辑,如下所示:

public class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
  public void uncaughtException(Thread t, Throwable e) {
     // Write the custom logic here
   }
}

And set it using any of the three options I have described in the above link.并使用我在上述链接中描述的三个选项中的任何一个进行设置。 For example, you could do the following to set the default handler for the entire JVM (so any uncaught exception thrown will be handled by this handler).例如,您可以执行以下操作来为整个 JVM 设置默认处理程序(因此任何未捕获的异常都将由该处理程序处理)。

Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler() );
try {
   // do something
   methodWithException();
}
catch (Throwable t) {
   showMessage(t);
}

}//end business method

private void showMessage(Throwable t){
  /* logging the stacktrace of exception
   * if it's a web application, you can handle the message in an Object: es in Struts you can use ActionError
   * if it's a desktop app, you can show a popup
   * etc., etc.
   */
}

Show the friendly message from within the catch block.catch块中显示友好消息。

you could wrap each method that can throw in a try catch你可以包装每个可以抛出 try catch 的方法

or use the getStackTrace()或使用getStackTrace()

catch (Throwable t) {
    StackTraceElement[] trace = t.getStackTrace();
    //trace[trace.length-1].getMethodName() should contain the method name inside the try
}

btw catching throwable in not recommended顺便说一句,不推荐使用可投掷物

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM