0
点赞
收藏
分享

微信扫一扫

Java中的异常处理 #yyds干货盘点#

1、类关系图

在这里插入图片描述
在这里插入图片描述

2、try-catch-finally

语法:

try{
    执行过程中可能出现错误的代码
} catch(可能出现的异常类型1 ){
   异常处理方案
} catch(可能出现的异常类型2 ){
   异常处理方案
} catch(可能出现的异常类型3 ){
   异常处理方案
} finally {
   无论是否发生异常都会执行的代码
}

示例1:

public static void main(String[] args) {
    try {
        System.out.println(3/0);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        System.out.println("finally");
    }
}
  • 结果:
    在这里插入图片描述

示例2:

 public static void main(String[] args) {
    File file = new File("pom.xml");
    FileReader reader = null;
    try {
        reader = new FileReader(file);
        char[] buf = new char[128];
        int len = -1;
        while ((len = reader.read(buf)) != -1) {
            String res = new String(buf, 0, len);
            System.out.println(res);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

3、Multi-catch

语法:

try{
    执行过程中可能出现错误的代码
} catch(可能出现的异常类型1 |可能出现的异常类型2 |可能出现的异常类型3 …){
   异常处理方案
} 

示例:

public static void main(String[] args) {
    int d = new Scanner(System.in).nextInt();
    try {
        System.out.println(3 / d);
        new File("G:/dd/aa/ab.txt").createNewFile();
    } catch (ArithmeticException e) {
        e.printStackTrace();
    } catch (IOException e) {
        // System.out.println(e.getMessage());
        e.printStackTrace();
    }
    //可以简写为
    try {
        System.out.println(3 / d);
        new File("G:/dd/aa/ab.txt").createNewFile();
    } catch (ArithmeticException | IOException e) {
        e.printStackTrace();
    }
}

4、try-with-resources

语法:

Try(待释放的资源的创建语句){ //创建资源时可能会抛出异常
    执行过程中可能出现错误的代码
} catch(可能出现的异常类型1 |可能出现的异常类型2 |可能出现的异常类型3 …){
   异常处理方案
} 

示例1: 系统会自动释放资源

 public static void main(String[] args) {
    File file = new File("pom.xml");
    try (FileReader reader = new FileReader(file);) {
        char[] buf = new char[128];
        int len = -1;
        while ((len = reader.read(buf)) != -1) {
            String res = new String(buf, 0, len);
            System.out.println(res);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

示例2:

public static void main(String[] args) {
    try (FileReader reader = new FileReader(new File("E:/data.txt"));
            FileWriter writer = new FileWriter(new File("E:/abc.txt"));) {
        char[] buf = new char[16];
        int len = -1;
        while ((len = reader.read(buf)) != -1) {
            String str = new String(buf, 0, len);
            writer.write(str);
        }
    } catch (Exception e) {
        System.out.println("cuo le");
    }
}

5、抛出异常

示例1:

 public static void main(String[] args) throws IOException {
        File file = new File("pom.xml");
        FileReader reader = new FileReader(file);
        char[] buf = new char[128];
        int len = -1;
        while((len = reader.read(buf))!= -1){
            String res = new String(buf, 0, len);
            System.out.println(res);
        }
        reader.close();
    }

示例2:

 public static void main(String[] args) throws IOException {
    FileReader reader = new FileReader(new File("E:/data.txt"));
    char[] buf = new char[16];
    int len = -1;
    while((len = reader.read(buf)) != -1) {
        String str = new String(buf,0,len);
        System.out.println(str);
    }
    reader.close();
}

示例3:

 public static void main(String[] args) throws IOException {
    FileReader reader = new FileReader(new File("E:/data.txt"));
    FileWriter writer = new FileWriter(new File("E:/abc.txt"));
    char[] buf = new char[16];
    int len = -1;
    while ((len = reader.read(buf)) != -1) {
        String str = new String(buf, 0, len);
        writer.write(str);
    }
    if (reader != null) {
        reader.close();
    }
    if (writer != null) {
        writer.close();
    }
}
举报

相关推荐

0 条评论