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 @   Gazikel  阅读(89)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示