20200312Java学习笔记:异常

异常Exception

1. 什么是异常

异常(Exception)值程序运行中出现的各种导致程序中断的状况:如文件找不到、网络连接失败、非法参数等

异常发生在程序运行期间,它影响了正常的程序执行流程。

2.异常分类

简单分为:检查性异常、运行时异常、错误。

  • 检查性异常:CheckedException
    检查性异常即必须进行处理的异常,要么try catch住,要么往外抛,谁调用,谁处理,比如 FileNotFoundException。如果不处理,编译器,就不让你通过。

  • 运行时异常RuntimeException:

    不是必须进行try catch的异常

    常见运行时异常:
    算数异常,如除数不能为0:ArithmeticException

    下标越界异常:ArrayIndexOutOfBoundsException

    空指针异常:NullPointerException

    找不到类:ClassNotFoundException

    丢失资源:MIssingResourceException

    这些异常通常是由于程序的逻辑错误造成的,在编写代码的时候,依然可以使用try catch throws进行处理,与可查异常不同之处在于,即便不进行try catch,也不会有编译错误。Java之所以会设计运行时异常的原因之一,是因为下标越界,空指针这些运行时异常太过于普遍,如果都需要进行捕捉,代码的可读性就会变得很糟糕。在程序中,应该尽可能去处理这些异常。

  • 错误:错误是脱离程序员控制的系统级别的异常,是程序无法控制和处理的。一旦出现错误,Java虚拟机JVM会终止线程。错误在代码中通常被忽略。例如内存用完了,在编译时也检查不到。与运行时异常一样,错误也是不要求强制捕捉的。但也可以被捕获。

3. 异常体系结构

Throwable是类,Exception和Error都继承了该类,所以在捕捉的时候,也可以使用Throwable进行捕捉。如图: 异常分ErrorException,Exception里又分运行时异常可查异常

在这里插入图片描述

package exception;

public class Test {
    public static void main(String[] args) {
        int a=1;
        int b=0;
    //try监控区域
    try {
        System.out.println(a/b);
    }
    //catch 捕获异常catch(想要捕获的异常类型)
        catch (Exception e) {
            System.out.println("程序出现异常,分母不能为零");;
    }
        finally {
        System.out.println("finally");//在这里可以不要
    }
    }
}

假设要捕获多个异常,范围要从小到大

package exception;

public class Test {
    public static void main(String[] args) {
        int a=1;
        int b=0;
    //try监控区域
    try {
        System.out.println(a/b);
    }
    //catch 捕获异常 catch(异常类型)
    //捕获多种类型,范围从小到大
        catch (Error e)//error也可以被捕获
        {
            System.out.println("Error!");
        }
        catch (Exception e) {
            System.out.println("Exception!");
    }
        catch (Throwable t){
            System.out.println("Throwable!");
        }
        finally {
        System.out.println("finally");//在这里可以不要
    }
    }
}

4. 处理异常

抛出异常和捕获异常

**异常处理五个关键字:**try,catch,finally,throw,throws

try catch:捕获它

  1. 将可能抛出FileNotFoundException 文件不存在异常的代码放在try里。
  2. 如果文件存在,就会顺序往下执行,并且不执行catch块中的代码。
  3. 如果文件不存在,try 里的代码会立即终止,程序流程会运行到对应的catch块中。
  4. e.printStackTrace(); 会打印出方法的调用痕迹,如此例,会打印出异常开始于Test3的第12行,这样就便于定位和分析到底哪里出了异常。
package exception;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class Test3 {
    public static void main(String[]args){
        File file=new File("C:\\Users\\Princekin Yan\\Desktop\\Java学习笔记\\日期0.md");
        try{
            System.out.println("试图打开文件中");
            new FileInputStream(file);
            System.out.println("文件已打开");
        } catch (FileNotFoundException e) {
            System.out.println("文件找不到");
            e.printStackTrace();
        }
    }
}

使用异常的父类进行catch:捕获它

FileNotFoundException是Exception的子类,使用Exception也可以catch住FileNotFoundException

package exception;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class Test03 {
    public static void main(String[]args){
        File file=new File("C:\\Users\\Princekin Yan\\Desktop\\Java学习笔记\\日期0.md");
        try{
            System.out.println("试图打开文件中");
            new FileInputStream(file);
            System.out.println("文件已打开");
        } catch (Exception e)
        //FileNotFoundException是Exception的子类,使用Exception也可以catch住FileNotFoundException      
        {
            System.out.println("文件找不到");
            e.printStackTrace();
        }
    }
}

多异常捕捉办法

有的时候一段代码会抛出多种异常,比如

new FileInputStream(f);
Date d = sdf.parse("2020-01-03");

这段代码,会抛出 文件不存在异常 FileNotFoundException 和 解析异常ParseException
解决办法之一是分别进行catch。

package exception;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test04 {
    public static void main(String[]args){
        File file=new File("C:\\Users\\Princekin Yan\\Desktop\\Java学习笔记\\日期0.md");
        try{
            System.out.println("试图打开文件中");
            new FileInputStream(file);
            System.out.println("文件已打开");
            String string="2020-03-12";
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            Date d=sdf.parse(string);
        } catch (FileNotFoundException e)
        {
            System.out.println("文件找不到");
            e.printStackTrace();
        }
        catch (ParseException e){
            System.out.println("编译错误");
            e.printStackTrace();
        }
    }
}


也可以放在一个catch里统一捕捉,这种方式从 JDK7开始支持,好处是捕捉的代码更紧凑,不足之处是,一旦发生异常,不能确定到底是哪种异常,需要通过instanceof 进行判断具体的异常类型,真正的缺点是,在这里每次只能发现一个异常。

package exception;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test04 {
    public static void main(String[]args){
        //故意写一个异常
        File file=new File("C:\\Users\\Princekin Yan\\Desktop\\Java学习笔记\\日期0.md");
        try{
            String string="2020-03-12";
            //故意写一个异常
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM/dd");
            Date d=sdf.parse(string);
            //在try中遇到异常之后,就会直接跳到catch中的语句,不再继续执行try的后续语句
            // 因此在这里一次只能找到一个异常
            System.out.println("试图打开文件中");
            new FileInputStream(file);
            System.out.println("文件已打开");

        } catch (FileNotFoundException | ParseException e)
        {
            if(e instanceof FileNotFoundException){
            System.out.println("文件找不到");
            e.printStackTrace();
            }
            else if(e instanceof ParseException){
                System.out.println("编译错误");
                e.printStackTrace();
            }
        }
    }
}

throws和throw:抛出

  1. 语句抛出的异常
    用户程序自定义的异常和应用程序特定的异常,必须借助于 throws 和 throw 语句来定义抛出异常。

    • throw是语句抛出一个异常。语法:throw 异常对象=>throw e;

    • throws是方法可能抛出异常的声明。(用在声明方法时,表示该方法可能要抛出异常)

      语法:修饰符 返回值类型 方法名 (参数列表) throws 异常类{…}
      public void do(int a) throws Exception1,Exception3{…}

  2. throw和throws的区别:

    • throws 出现在方法声明上,表示再抛出异常,由该方法的调用者来处理。而throw通常都出现在方法体内,表示抛出异常,由方法体内的语句处理。。

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

    • throws主要是声明这个方法会抛出这种类型的异常,使它的调用者知道要捕获这个异常。
      throw是具体向外抛异常的动作,所以它是抛出一个异常实例。

    • 两者都是消极处理异常的方式(这里的消极并不是说这种方式不好),只是抛出或者可能抛出异常,但是不会由函数去处理异常,真正的处理异常由函数的上层调用处理。

例如:

考虑如下情况:
主方法调用method1,method1调用method2,method2中打开文件,那么method2中需要进行异常处理。但是method2不打算处理,而是把这个异常通过throws抛出去。那么method1就会接到该异常。 处理办法也是两种,要么是try catch处理掉,要么也是抛出去。method1选择本地try catch住 一旦try catch住了,就相当于把这个异常消化掉了,主方法在调用method1的时候,就不需要进行异常处理了。

package exception;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
//throws可理解为把异常一层一层往外抛出,直到将它处理。
public class Test05 {
   private static void method2() throws FileNotFoundException {
       File file=new File("C:\\Users\\Princekin Yan\\Desktop\\Java学习笔记\\日期0.md");
       new FileInputStream(file);
   }
    private static void method1(){
       try {
           method2();
       }
       catch (FileNotFoundException e){
           e.printStackTrace();
       }
    }

    public static void main(String[] args) {
        method1();
    }
}

​ 主方法调用method1,method1调用method2,method2中打开文件和转换日期格式,那么method2中需要进行异常处理。程序中可能有编译错误和找不到文件两种异常。找不到文件异常在method2中已经自行处理,所以FileNotFoundException在方法头中就不用写出了,如果写出,则三个方法头中都要写出。

​ 编译异常则由method2抛出,给调用它的method1,在method1中又继续抛出,在调用它的main方法中进行处理。

package exception;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test06 {
    private static void method2() throws ParseException{
        File file=new File("C:\\Users\\Princekin Yan\\Desktop\\Java学习笔记\\日期.md");
       try {
           new FileInputStream(file);
           System.out.println("文件已打开");
           String string = "2020-03-12";
           //故意写一个异常
           SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM/dd");
           Date d = sdf.parse(string);
       } catch (FileNotFoundException e) {
           System.out.println("文件找不到");
           e.printStackTrace();//自行处理
       }
         catch (ParseException e){
           throw e;//抛出,给调用它的函数处理
         }
    }
    private static void method1() throws ParseException{
        try {
            method2();
        }
        catch (ParseException e){
            throw e;//继续抛出,给调用method1的方法处理
        }
    }

    public static void main(String[] args) throws ParseException {
        try {
            method1();
        }
        catch (ParseException e){
            System.out.println("编译出错");
            e.printStackTrace();
        }
    }
}

finally:

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

package exception;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class Test03 {
    public static void main(String[]args){
        File file=new File("C:\\Users\\Princekin Yan\\Desktop\\Java学习笔记\\日期0.md");
        try{
            System.out.println("试图打开文件中");
            new FileInputStream(file);
            System.out.println("文件已打开");
        } catch (Exception e)
        //FileNotFoundException是Exception的子类,使用Exception也可以catch住FileNotFoundException
        {
            System.out.println("文件找不到");
            e.printStackTrace();
        }
        finally{
            System.out.println("无论是否有异常,都会执行finally!");
        }
    }
}

5. 自定义异常

在这里插入图片描述

  • 创建自定义异常:

    创建一个自己的异常类,如AgeException,并继承Exception。
    提供两个构造方法
    1.无参的构造方法
    2.带参的构造方法,并调用父类的对应的构造方法

    package exception;
    
    public class AgeException extends Exception{
        public AgeException(){}
        public AgeException(String msg){
            super(msg);
        }
    }
    
    
    package exception;
    
    import exception.AgeException;
    
    public class Test07 {
        public static void main(String[] args) {
            People people = new People();
            try {
                people.setAge(-1);
            }catch(AgeException e){
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
        }
    }
    
        class People {
         int age;
    
        public int getAge() {
            return age;
        }
        public void setAge(int age) throws AgeException {
            if (age < 0 || age > 150) {
                throw new AgeException("年龄输入有误");
            }
            this.age = age;
        }
    }
    
    
    

6.总结

在这里插入图片描述

这是一个类图
Account类: 银行账号
属性: balance 余额
方法: getBalance() 获取余额
方法: deposit() 存钱
方法: withdraw() 取钱
OverdraftException: 透支异常,继承Exception
属性: deficit 透支额

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JfwoixEG-1584061972523)(C:\Users\Princekin Yan\AppData\Roaming\Typora\typora-user-images\image-20200312230859087.png)]

package exception;

public class Amount {
    public double balance;
    public Amount(int balance){
        this.balance=balance;
    }
    public double getBalance(){
        return balance;
    }
    public void deposit(double depositedMoney){
        balance+=depositedMoney;
    }
    public void withdraw (double withdrawedMoney) throws OverdraftException{
        balance-=withdrawedMoney;
        if(balance<0){
            throw new OverdraftException("余额不足",Math.abs(balance));
            //自定义类构造函数的作用在这里体现了。把透支额传回去。
        }
    }

    public static void main(String[] args) {
        Amount amount=new Amount(1000);
        try{
            amount.deposit(500);
            amount.withdraw(2000);
        } catch (OverdraftException e) {
            //调用get.Defivit(),得到透支额
            System.out.println("余额不足!透支金额"+e.getDeficit());
            e.printStackTrace();
        }
    }
}
class OverdraftException extends Exception{
    double deficit;
    public OverdraftException(){}
    public OverdraftException(double deficit){
        this.deficit=deficit;
    }
    public OverdraftException(String message,double deficit){
        super(message);
        this.deficit=deficit;
    }
    public double getDeficit() {
        return deficit;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值