简体   繁体   English

抛出异常以及捕获异常?

[英]Throwing exceptions as well as catching exceptions?

I was wondering how Java takes the following scenario 我想知道Java如何采用以下方案

public static void main(String[] args) throws IndexOutOfBoundsException, CoordinateException, MissionException, SQLException, ParserConfigurationException {
    try {
        doSomething();
    } catch (Exception e) {
        e.printStackTrace();
    } 
}

In the above code, I am declaring the main function to throw many different exceptions, but inside the function, I am catching the generic Exception. 在上面的代码中,我声明了抛出许多不同异常的main函数,但在函数内部,我正在捕获泛型异常。 I am wondering how java takes this internally? 我想知道java内部如何使用它? Ie, say doSomething() throws an IndexOutOfBounds exception, will the e.printStackTrace() get called in the last catch (Exception e) {...} block? 即, doSomething()抛出一个IndexOutOfBounds异常,e.printStackTrace()会在最后一个catch (Exception e) {...}块中被调用吗?

I know if an exception not declared in the throws area of the function gets thrown, the try/catch will handle it, but what about exceptions mentioned in the declaration? 我知道如果函数的throws区域中没有声明的异常被抛出,try / catch将处理它,但是声明中提到的异常呢?

In your case if ANY Exception is thrown or generated in doSomething() it will be caught in the try-catch block because of the Exception e you are catching. 在你的情况下,如果在doSomething()抛出或生成任何异常,它将被捕获在try-catch块中,因为你正在捕获Exception e

Exception is the parent of all Exceptions. Exception是所有例外的父级 All Exceptions inherit from this class. 所有异常都继承自此类。

The catch block has greater priority over the method level throw declarations. catch块比方法级别throw声明具有更高的优先级。 If something would pass by that catch block, it would be thrown by the method (but that's not the case since all your mentioned exceptions are indeed inheriting from the Exception ). 如果某个东西会被该catch块传递,那么它将被该方法抛出(但事实并非如此,因为所有提到的异常确实都是从Exception继承的)。

If you need an exception to be handled by the catch block but forwarded further, you would have to rethrow it, eg 如果你需要一个由catch块处理但是进一步转发的异常,你将不得不重新抛出它,例如

throw e;

say doSomething() throws an IndexOutOfBounds exception, will the e.printStackTrace() get called in the last catch (Exception e) {...} block? 假设doSomething()抛出一个IndexOutOfBounds异常,e.printStackTrace()会在最后一个catch(Exception e){...}块中被调用吗?

Yes, e.printStackTrace() will be called. 是的,将调用e.printStackTrace() Because you have caught Exception which is broader than (either direct or indirect super class of ) IndexOutOfBoundException or any of the other exceptions that you have put in throws clause. 因为您捕获的Exception比( Exception直接或间接超类) IndexOutOfBoundException或您在throws子句中放置的任何其他异常更广泛。 But if you catch some exceptions which is narrower than IndexOutOfBoundException and other exceptions in throws clauses and any of these exceptions is encountered then throws clause will trigger. 但是,如果您捕获一些比IndexOutOfBoundException更窄的异常以及throws子句中的其他异常,并且遇到任何这些异常,那么将触发throws子句。

Suppose you have this code: 假设你有这个代码:

   try{
        doSomething();
    }catch(Exception ex){
        System.out.println("Exception:");
        ex.printStackTrace();
    }catch(IndexOutOfBoundsException ex){
         System.out.println("IndexOutOfBoundsException :");
        ex.printStackTrace();
    }

You will get a compile error saying 您将收到编译错误说明

Exception IndexOutIfBounds is already been caught 异常IndexOutIfBounds已被捕获

That's because every exceptions inherits from Exception class so the first thing that catch block catches is the Exception because IndexOutOfBounds is an Exception so not needed to catch it twice one for the first and one for the second. 这是因为每个异常都Exception类继承,所以捕获块捕获的第一件事就是异常,因为IndexOutOfBounds是一个Exception,因此不需要为第一个捕获两次,第二次捕获一次。
In one case the throws will do its work when you don't declare an exception class after throws or in the try-catch. 在一种情况下,当您在throws之后或try-catch中未声明异常类时,throws将执行其工作。

The exceptions, mentioned in the declaration, is to allow a method further up the call stack to handle it. 声明中提到的异常是允许进一步调用堆栈的方法来处理它。 In your case these exceptions, if your main method throws them, will get handled by runtime. 在您的情况下,如果您的main方法抛出它们,这些异常将由运行时处理。

Source http://docs.oracle.com/javase/tutorial/essential/exceptions/declaring.html 来源http://docs.oracle.com/javase/tutorial/essential/exceptions/declaring.html

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

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