how to throw an IOException?

前端 未结 7 1707
天涯浪人
天涯浪人 2020-12-19 04:17
public class ThrowException {
    public static void main(String[] args) {
        try {
            foo();
        }
        catch(Exception e) {
             if (e         


        
7条回答
  •  囚心锁ツ
    2020-12-19 05:12

    Maybe this helps...

    Note the cleaner way to catch exceptions in the example below - you don't need the 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());
        }
    }
    

提交回复
热议问题