Java日志第34天 2020.8.8

异常

异常的处理

throws

import java.io.FileNotFoundException;

public class Demo01Throws {

    public static void main(String[] args) throws FileNotFoundException {
        readFile("C:\\b.txt");
    }

    public static void readFile(String name) throws FileNotFoundException {
        if(name != "C:\\a.txt"){
            throw new FileNotFoundException();
        }
        System.out.println("文件名正确!");
    }
}

 

try...catch

import java.io.FileNotFoundException;

public class Demo01Throws {

    public static void main(String[] args) {
        try {
            //写出可能会抛出错误的代码
            readFile("C:\\b.txt");
        } catch (FileNotFoundException e) {
            System.out.println(e);
        }
    }

    public static void readFile(String name) throws FileNotFoundException {
        if(name != "C:\\a.txt"){
            throw new FileNotFoundException();
        }
        System.out.println("文件名正确!");
    }
}

 

Throwable类中三个异常处理方法

1. public String getMessage()  返回此throwable的简短描述

import java.io.FileNotFoundException;

public class Demo01Throws {

    public static void main(String[] args) {
        try {
            //写出可能会抛出错误的代码
            readFile("C:\\b.txt");
        } catch (FileNotFoundException e) {
            System.out.println(e.getMessage());//返回此throwable的简短描述
        }
        System.out.println("后续代码");
    }

    public static void readFile(String name) throws FileNotFoundException {
        if(name != "C:\\a.txt"){
            throw new FileNotFoundException("文件名错误!");
        }
        System.out.println("文件名正确!");
    }
}

 

2. String toString() 返回此throwable的详细消息字符串

import java.io.FileNotFoundException;

public class Demo01Throws {

    public static void main(String[] args) {
        try {
            //写出可能会抛出错误的代码
            readFile("C:\\b.txt");
        } catch (FileNotFoundException e) {
            System.out.println(e.toString());//返回此throwable的详细消息字符串
        }
        System.out.println("后续代码");
    }

    public static void readFile(String name) throws FileNotFoundException {
        if(name != "C:\\a.txt"){
            throw new FileNotFoundException("文件名错误!");
        }
        System.out.println("文件名正确!");
    }
}

结果如下:

3. void printStackTrace() JVM打印异常对象,默认此方法,打印的异常信息是最全面的

import java.io.FileNotFoundException;

public class Demo01Throws {

    public static void main(String[] args) {
        try {
            //写出可能会抛出错误的代码
            readFile("C:\\b.txt");
        } catch (FileNotFoundException e) {
            e.printStackTrace();//JVM打印异常对象,默认此方法,打印的异常信息是最全面的
        }
        System.out.println("后续代码");
    }

    public static void readFile(String name) throws FileNotFoundException {
        if(name != "C:\\a.txt"){
            throw new FileNotFoundException("文件名错误!");
        }
        System.out.println("文件名正确!");
    }
}

结果如下:

 

 

finally代码块

finally代码块中的代码无论是否出现异常都会执行

注意事项:

1.finally不能单独使用,必须与try一起使用

2.finally一般用于资源释放(资源回收),无论程序是否出现异常,最后都要资源释放(IO)

import java.io.FileNotFoundException;

public class Demo01Throws {

    public static void main(String[] args) {
        try {
            //写出可能会抛出错误的代码
            readFile("C:\\b.txt");
        } catch (FileNotFoundException e) {
            e.printStackTrace();//JVM打印异常对象,默认此方法,打印的异常信息是最全面的
        } finally {
            System.out.println("资源释放");
        }
        System.out.println("后续代码");
    }

    public static void readFile(String name) throws FileNotFoundException {
        if(name != "C:\\a.txt"){
            throw new FileNotFoundException("文件名错误!");
        }
        System.out.println("文件名正确!");
    }
}

 

 

 

这一次内容实际上已经学习过了,但是那次的感觉就是很蒙,这一次就比较好。

*问题:throw 与 throws有什么区别,除了位置不同?

  throw

1、throw是语句抛出一个异常,一般是在代码块的内部,当程序

现某种逻辑错误时由程序员主动抛出某种特定类型的异常

2、定义在方法体内

3、创建的是一个异常对象

4、确定了发生哪种异常才可以使用

 throws

1、在方法参数列表后,throws后可以跟着多个异常名,表示抛出的异常用逗号隔开

2、表示向调用该类的位置抛出异常,不在该类解决

3、可能发生哪种异常

 

throws用在方法声明后面,跟的是异常类名,throw用在方法体内,跟的是异常对象名。

     throws可以跟多个异常类名,用逗号隔开,throw只能抛出一个异常对象名。

     throws表示抛出异常,由该方法的调用者来处理,throw表示抛出异常,由方法体内的语句处理。

throws表示出现异常的一种可能性,并不一定会发生这些异常,throw则是抛出了异常,执行throw则一定抛出了某种异常。

 

明日任务:

1.将异常的知识全部学习完

2.将异常的练习题做完

3.预习第12章《用I/O进行数据处理》

posted @ 2020-08-08 19:10  Gazikel  阅读(87)  评论(0编辑  收藏  举报