Java 如何抛出 IOException?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9719993/
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-08-16 06:41:09  来源:igfitidea点击:

how to throw an IOException?

javaexceptionio

提问by

public class ThrowException {
    public static void main(String[] args) {
        try {
            foo();
        }
        catch(Exception e) {
             if (e instanceof IOException) {
                 System.out.println("Completed!");
             }
          }
    }
    static void foo() {
        // what should I write here to get an exception?
    }
}

Hi! I just started learning exceptions and need to catch an expetion, so please can anybody provide me with a solution? I'd be very grateful. Thanks!

你好!我刚开始学习异常,需要抓住一个expetion,所以请谁能给我一个解决方案?我会很感激。谢谢!

回答by juergen d

static void foo() throws IOException {
    throw new IOException("your message");
}

回答by Chandra Sekhar

try {
        throw new IOException();
    } catch(IOException e) {
         System.out.println("Completed!");
    }

回答by MichaBa

throw new IOException("Test");

回答by Sérgio Michels

I just started learning exceptions and need to catch an exception

我刚开始学习异常,需要捕捉异常

To throw an exception

抛出异常

throw new IOException("Something happened")

To catch this exception is better not use Exceptionbecause is to much generic, instead, catch the specific exception that you know how to handle:

捕捉这个异常最好不要使用,Exception因为它太通用了,相反,捕捉你知道如何处理的特定异常:

try {
  //code that can generate exception...
}catch( IOException io ) {
  // I know how to handle this...
}

回答by ewok

If the goal is to throw the exception from the foo()method, you need to declare it as follows:

如果目标是从foo()方法中抛出异常,则需要如下声明:

public void foo() throws IOException{
    \do stuff
    throw new IOException("message");
}

Then in your main:

然后在你的主要:

public static void main(String[] args){
    try{
        foo();
    } catch (IOException e){
        System.out.println("Completed!");
    }
}

Note that, unless foo is declared to throw an IOException, attempting to catch one will result in a compiler error. Coding it using a catch (Exception e)and an instanceofwill prevent the compiler error, but is unnecessary.

请注意,除非 foo 被声明为抛出 IOException,否则尝试捕获一个将导致编译器错误。使用 acatch (Exception e)和 an对其进行编码instanceof将防止编译器错误,但这是不必要的。

回答by Canton

Maybe this helps...

也许这有助于...

Note the cleaner way to catch exceptions in the example below - you don't need the e instanceof IOException.

请注意以下示例中捕获异常的更简洁方法 - 您不需要e instanceof IOException.

public static void foo() throws IOException {
    // some code here, when something goes wrong, you might do:
    throw new IOException("error message");
}

public static void main(String[] args) {
    try {
        foo();
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
}

回答by Damien M.

Please try the following code:

请尝试以下代码:

throw new IOException("Message");