简体   繁体   English

在哪里捕获和处理此异常?

[英]Where is this exception caught and handled?

In some code I've been reading, I've come across this : 在我读过的一些代码中,我遇到了这个问题:

    class Someclass
    {
      public static void main(String[] args) throws IOException
      {
        //all other code here......
      }
    }

If main() throws an exception, in this case its an IOException, where is it caught and handled? 如果main()引发异常,在这种情况下为IOException,则在哪里捕获和处理该异常?

EDIT: Is this considered bad practice? 编辑:这被认为是不良做法吗? Or is this really common in real world code? 还是这在现实世界的代码中真的很常见?

The detailed flowchart of full uncaught exception handling is given here: How uncaught exceptions are handled in Java . 完整的未捕获异常处理的详细流程图在此处给出: Java中未捕获异常的处理方式

When an uncaught exception occurs, the JVM does the following: 发生未捕获的异常时,JVM将执行以下操作:

  • it calls a special private method, dispatchUncaughtException() , on the Thread class in which the exception occurs; 它在发生异常的Thread类上调用特殊的私有方法dispatchUncaughtException()
    • [...which] calls the thread's getUncaughtExceptionHandler() method to find out the appropriate uncaught exception handler to use. [...哪个]调用线程的getUncaughtExceptionHandler()方法以查找要使用的适当的未捕获异常处理程序。 Normally, this will actually be the thread's parent ThreadGroup , whose handleException() method by default will print the stack trace. 通常,这实际上是线程的父ThreadGroup ,其默认情况下的handleException()方法将打印堆栈跟踪。
  • it then terminates the thread in which the exception occurred. 然后,它终止发生异常的线程。

Therefore you can, if you wish to, create your own custom uncaught exception handler. 因此,您可以根据需要创建自己的自定义未捕获异常处理程序。

It should also be noted that while main is commonly used as a Java application entry point, the method is just like any other methods in that it can also be called from other contexts (eg other main methods, or even itself recursively!). 还应当指出的是,虽然main是常用的Java应用程序的入口点,方法就像任何其他的方法,它也可以从其他情况下调用(例如:其他main方法,甚至本身递归!)。 In that case, the caller can catch exceptions thrown. 在这种情况下,调用方可以捕获引发的异常。

public class SelfCatch {
    public static void main(String args[]) throws Exception {
        if (args == null) throw new Exception("Hi there!");
        try {
            main(null);
        } catch (Exception e) {
            System.out.println("Caught: " + e);
        }
        System.out.println("Exiting...");
    }
}

Output: 输出:

Caught: java.lang.Exception: Hi there!
Exiting...

EDIT: Is this considered bad practice? 编辑:这被认为是不良做法吗? Or is this really common in real world code? 还是这在现实世界的代码中真的很常见?

It would be in production code, but when rapid prototyping or knocking up test code its often used as its quicker than typing the try {...} catch block. 它将在生产代码中,但是当快速制作原型或删除测试代码时,通常比输入try {...} catch块更快。 (unless you use a good IDE like Eclipse 3.5 which has an 'Auto wrap in try/catch' feature [auto-detecting any and all exceptions to!] ;-) ) (除非您使用像Eclipse 3.5这样的优秀IDE,它具有“自动包裹try / catch”功能[自动检测到任何和所有异常!] ;-))

Or your pretty sure it wont be thrown by methods invoked by main(). 或者您可以肯定,它不会被main()调用的方法抛出。

But even wrapping in a try/catch block will usually result in the same output as if you leave the Exception uncaught, if you simply use e.printStackTrace() ... 但是,即使只包装在try / catch块中,如果您仅使用e.printStackTrace(),通常也会获得与未捕获Exception相同的输出。

At the command line. 在命令行。

EDIT: The entry point is Main . 编辑:入口点是Main Hence, there is no other method/caller to handle the exception. 因此,没有其他方法/调用者可以处理该异常。

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

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