如何抛出IOException?

7 java io exception

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?
    }
}
Run Code Online (Sandbox Code Playgroud)

嗨!我刚开始学习例外并且需要赶上一个考试,所以请有人能为我提供解决方案吗?我会非常感激的.谢谢!

jue*_*n d 19

static void foo() throws IOException {
    throw new IOException("your message");
}
Run Code Online (Sandbox Code Playgroud)


Cha*_*har 6

try {
        throw new IOException();
    } catch(IOException e) {
         System.out.println("Completed!");
    }
Run Code Online (Sandbox Code Playgroud)