Java(十五) -- 异常

目录

异常

什么是异常?

异常的使用

异常类

RuntimeException 常见的子类

throw 和 throws

Java 中抛出异常有 3 种方式

异常捕获

自定义异常

综合练习


异常

  • 什么是异常?

        Java 中的错误大致可以分为两类:

                一类是编译时错误,一般是指语法错误

                另一类是运行时错误

        Java 中有一组专门用来描述各种不同的运行时异常,叫做异常类,Java 结合异常类提供了处理错误的机制。

        具体步骤是当程序出现错误时,会创建一个包含错误信息的异常类的实例化对象,并自动将该对象提交给系统,由系统转交给能够处理异常的代码进行处理。

        异常可以分为两类:ErrorException

                Error 是指系统错误,JVM 生成,我们编写的程序无法处理。

                Exception 指程序运行期间出现的错误,我们编写的程序可以对其进行处理。

  • 异常的使用

        异常的使用需要用到两个关键字 try 和 catch,并且这两个关键字需要结合起来使用,用 try 来监听可能会抛出异常的代码,一旦捕获到异常,生成异常对象并交给 catch 来处理,基本语法如下所示。

try{
    //可能抛出异常的代码
}catch(Exception e){
    //处理异常
}
public class Test {
    public static void main(String[] args) {
        try {
            int num = 10/10;
        }catch (Exception e) {
            // TODO: handle exception
            if(e.getMessage().equals("/ by zero")) {
                System.err.println("分母不能为0");
            }
        }
    }
}

        除了 try 和 catch,还可以使用 finally 关键字来处理异常,finally 的作用?

                无论程序是否抛出异常,finally 代码块中的代码一定都会执行,finally 一般跟在 catch 代码块的后面,基本语法如下所示。

try{
    //可能抛出异常的代码
}catch(Exception e){
    //处理异常
}finally{
    //必须执行的代码
}
  • 异常类

        Java 将运行时出现的错误全部封装成类,并且不是一个类,而是一组类。同时这些类之间是有层级关系的,由树状结构一层层向下分级,处在最顶端的类是 Throwable,是所有异常类的根结点。

  • Error:系统错误,程序无法处理。

  • Exception:程序运行时出现的错误,程序可以处理。

        Error 和 Exception 都是 Throwable 的子类,Throwable、Error、Exception 都是存放在 java.lang 包中。

        Error 常见的子类:VirtualMachineError、AWTError、IOError。

        VirtualMachineError 的子类:StackOverflowError、OutOfMemoryError,用来描述内存溢出等系统问题。

        Exception 常见的子类:IOException 和 RuntimeException

        IOException 存放在 java.io 包中,RuntimeException 存放在 java.lang 包中。

        IOException 常见的子类:FileLockInterruptionException、FileNotFoundException、FilerException,这些异常通常都是处理通过 IO 流进行文件传输的时候发生的错误。

RuntimeException 常见的子类

  • ArithmeticException:表示数学运算异常。

  • ClassNotFoundException:表类未定义异常。

  • IllelArgumentException:表示参数格式错误。

  • ArrayIndexOutOfBounds:表示数组下标越界。

  • NullPointException:表示空指针异常。

  • NoSuchMethodException:表示方法未定义异常。

  • NumberFormatException:表示将其他数据类型转为数值类型发生的类型不匹配异常。

throw 和 throws

        throw 和 throws 是 Java 在处理异常时使用的两个关键字,都可以用来抛出异常,但是使用的方式和表示的含义完全不同。

Java 中抛出异常有 3 种方式

  • try-catch

  • 使用 throw 是开发者主动抛出异常,即读到 throw 代码就一定抛出异常,基本语法:throw new Exception(),是一种基于代码的逻辑而主动抛出异常的方式。

public class Test {
    public static void main(String[] args) {
        int[] array = {1,2,3};
        test(array,2);
    }
    
    public static void test(int[] array,int index) {
        if(index >= 3 || index < 0) {
            try {
                throw new Exception();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }else {
            System.out.println(array[index]);
        }
    }
}
  • try-catch 和 throw 都是作用于具体的逻辑代码,throws 是作用于方法的,用来描述方法可能会抛出的异常。

        如果方法 throws 的是 RuntimeException 异常或者其子类,外部调用时可以不处理,JVM 会处理。

        如果方法 throws 的是 Exception 异常或者其子类,外部调用时必须处理,否则报错。

public class Test {
    public static void main(String[] args) throws Exception {
        test("123");
    }
    
    public static void test(String str) throws Exception {
        int num = Integer.parseInt(str);
    }
    
}

异常捕获

  • 自动捕获 try-cath

  • throw 主动抛出异常

  • throws 修饰可能抛出异常的方法

自定义异常

        除了使用 Java 提供的异常外,也可以根据需求来自定义异常。

public class MyNumberException extends RuntimeException {
    public MyNumberException(String error) {
        super(error);
    }
}
public class Test {
    public static void main(String[] args){
        Test test = new Test();
        System.out.println(test.add("a"));
    }
    
    public int add(Object object){
        if(object instanceof Integer) {
            int num = (int)object;
            return ++num;
        }else {
            String error = "传入的参数不是整数类型";
            MyNumberException myNumberException = new MyNumberException(error);
            throw myNumberException;
        }
    }
}

综合练习

        封装、继承、多态、抽象、接口、异常完成一个汽车查询系统。

        需求描述:共有 3 种类型的汽车:小轿车、大巴车、卡车,其中小轿车的座位数是 4 座,大巴车座位数是 53 座,卡车座位数是 2 座,要求使用封装、继承、抽象来完成车辆的定义。

        可以对车辆信息进行修改,卡车可以运货但是载重量不能超过 12 吨,使用自定义异常来处理错误,小轿车和大巴车没有此功能,要求使用接口来实现。

  • Car
public abstract class Car {
    private String name;
    private String color;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public Car(String name, String color) {
        super();
        this.name = name;
        this.color = color;
    }
    public abstract String seatNum();
}
  • Sedan
public class Sedan extends Car {
​
    public Sedan(String name, String color) {
        super(name, color);
    }
​
    @Override
    public String seatNum() {
        // TODO Auto-generated method stub
        return "4座";
    }
    
}
  • Bus
public class Bus extends Car {
​
    public Bus(String name, String color) {
        super(name, color);
        // TODO Auto-generated constructor stub
    }
​
    @Override
    public String seatNum() {
        // TODO Auto-generated method stub
        return "53座";
    }
​
}
  • Truck
public class Truck extends Car implements Container {
    
    private int weight;
​
    public Truck(String name, String color,int weight) {
        super(name, color);
        this.weight = weight;
        // TODO Auto-generated constructor stub
    }
​
    @Override
    public String seatNum() {
        // TODO Auto-generated method stub
        return "2座";
    }
​
    @Override
    public int getweight() {
        // TODO Auto-generated method stub
        return this.weight;
    }
​
}
  • Container
public interface Container {
    public int getweight();
}
  • CarException
public class CarException extends Exception {
    public CarException(String error) {
        super(error);
    }
}
  • Test
import java.util.Scanner;
​
public class Test {
    private static Scanner scanner;
    private static Sedan sedan;
    private static Bus bus;
    private static Truck truck;
    private static Car[] cars;
    static {
        scanner = new Scanner(System.in);
        sedan = new Sedan("小轿车","黑色");
        bus = new Bus("大巴车","绿色");
        truck = new Truck("卡车","蓝色",2);
        cars = new Car[3];
        cars[0] = sedan;
        cars[1] = bus;
        cars[2] = truck;
    }
    
    public void showCars() {
        System.out.println("欢迎使用本汽车管理系统");
        System.out.println("车辆名称\t\t车辆颜色\t\t座位数\t\t载重量");
        for(Car car:cars) {
            if(car instanceof Truck) {
                Truck truck = (Truck)car;
                System.out.println(car.getName()+"\t\t"+car.getColor()+"\t\t"+car.seatNum()+"\t\t"+truck.getweight());
            }else {
                System.out.println(car.getName()+"\t\t"+car.getColor()+"\t\t"+car.seatNum()+"\t\t不能拉货");
            }
        }
        System.out.println("1.小轿车\t2.大巴车\t3.卡车");
        System.out.print("请选择要修改的车辆:");
        int num = scanner.nextInt();
        switch(num) {
            case 1:
                update("sedan");
                break;
            case 2:
                update("bus");
                break;
            case 3:
                update("truck");
                break;
            default:
                System.out.println("车辆不存在!");
                break;
        }
    }
    
    public void update(String type) {
        String name = null;
        String color = null;
        if(type.equals("sedan")) {
            System.out.print("输入车辆名称");
            name = scanner.next();
            System.out.print("输入车辆颜色");
            color = scanner.next();
            Sedan sedan = new Sedan(name,color);
            cars[0] = sedan;
        }
        if(type.equals("bus")) {
            System.out.print("输入车辆名称");
            name = scanner.next();
            System.out.print("输入车辆颜色");
            color = scanner.next();
            Bus bus = new Bus(name,color);
            cars[1] = bus;
        }
        if(type.equals("truck")) {
            System.out.print("输入车辆名称");
            name = scanner.next();
            System.out.print("输入车辆颜色");
            color = scanner.next();
            System.out.print("输入载重量");
            int weight = scanner.nextInt();
            if(weight > 12) {
                CarException carException = new CarException("卡车的载重量不能超过12吨");
                try {
                    throw carException;
                } catch (CarException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    return;
                }
            }
            Truck truck = new Truck(name,color,weight);
            cars[2] = truck;
        }
        showCars();
    }
    
    public static void main(String[] args) {
        Test test = new Test();
        test.showCars();
    }
}

        讲解了面向对象的高级部分,包括 Object 类、包装类、接口和异常。其中 Object 类是所有 Java 类的父类,定义了 Java 体系的基础资料,通过继承传递给 Java 的每一个类,通过方法重写和多态让整个 Java 体系具有很强的灵活性。

        包装类是 Java 为基本数据类型提供封装的一组类,通过包装类我们可以将基本数据类型转为对象,这一点在面向对象编程中很重要。

        接口是抽象类的扩展,是 Java 中实现多态的重要方式,可以降低程序的耦合性,让程序变得更加灵活多变。接口就相当于零件,我们可以自由地将这些零件进行组装、整合。

        异常是 Java 中处理错误的一种机制,同样是基于面向对象的思想,将错误抽象成对象然后进行处理,这里需要关注的是对异常相关的几个关键字的使用,try、catch、finally、throw、throws。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bug 消灭师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值