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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6243887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:59:22  来源:igfitidea点击:

Java Exceptions - Handling exceptions without try catch

javaexception-handling

提问by Devanath Rao

In Java, we handle exceptions using try catch blocks. I know that I can write a try catch block like the one below to catch any exception thrown in a method.

在 Java 中,我们使用 try catch 块处理异常。我知道我可以编写一个像下面这样的 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 Yohan Liyanage

By default, the JVM handles uncaught exceptions by printing the stack-trace to System.err stream. Java allows us to customize this behavior by providing our own routine which implements Thread.UncaughtExceptionHandlerinterface.

默认情况下,JVM 通过将堆栈跟踪打印到 System.err 流来处理未捕获的异常。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() );

回答by alepuzio

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.
   */
}

回答by ratchet freak

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

顺便说一句,不推荐使用 throwable

回答by Ted Hopp

Show the friendly message from within the catchblock.

显示来自catch块内的友好消息。