简体   繁体   中英

throws statement for handled exceptions — Java

Suppose the following code:

public static void somMethod() throws IOException {

try {
    // some code that can throw an IOException and no other checked exceptions
} catch (IOException e) {
        // some stuff here -- no exception thrown in this block
}

}

someMethod throws an IOException , and no other checked exception, and handles that exception itself.

What exactly

throws IOException 

in its declaration is bringing in? From what I know, it is making it possible for the methods calling someMethod() handle that IOException themselves.

is anything else happening here?

If the catch block doesn't throw IOException , the throws IOException part in the method signature is not necessary. And also, every time the someMethod() is invoked, there has to be provided a catch block for a possible exception that actually never occurs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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