Java编程:自定义异常案例(多个异常处理)

自定义异常

用户只需要继承Exception类即可自定义异常类。在程序中使用自定义异常类,大体可分为以下几个步骤:

1、创建自定义异常类。

2、在方法中通过throw关键字抛出异常对象。

3、如果在当前抛出异常的方法中处理异常,可以使用try-catch语句块捕获并处理,否则在方法的声明处通过throws关键字指明要抛出给方法调用者的异常,继续进行下一步操作。

4、在出现异常的方法的调用者中捕获并处理异常。

自定义异常案例

案例描述 老师上课用电脑 第一种:电脑死机异常 处理方式:影响老师上课,发生老师上课异常 处理:去修电脑 第二种:电脑蓝屏异常 处理方式:重启电脑 代码实现

package com.cr.instance;
/*
自定义异常案例:

老师上课用电脑
    第一种:电脑死机异常
        处理方式:影响老师上课,发生老师上课异常
            处理:去修电脑
    第二种:电脑蓝屏异常
        处理方式:重启电脑
 */
public class Instance03 {
          
   
    public static void main(String[] args) {
          
   
        Computer computer = new Computer();
        Teacher teacher = new Teacher("张三",computer);
        try{
          
   
            teacher.classing();
        }catch (ClassingException e){
          
   
            e.printStackTrace();
            System.out.println("去修电脑");
        }

    }
    //定义三个异常
    static class LanPingException extends Exception{
          
   
        public LanPingException(){
          
   }
        public LanPingException(String message) {
          
   
            super(message);
        }
    }
    static class SiJiException extends Exception{
          
   
        public SiJiException(){
          
   }
        public SiJiException(String message) {
          
   
            super(message);
        }
    }
    static class ClassingException extends Exception{
          
   
        public ClassingException(){
          
   }
        public ClassingException(String message) {
          
   
            super(message);
        }
    }
    //老师类
    static class Teacher{
          
   
        private String name;
        private Computer computer;

        public Teacher(String name, Computer computer) {
          
   
            this.name = name;
            this.computer = computer;
        }
        //老师上课
        public void classing()throws ClassingException{
          
   
            try{
          
   
                computer.work(2);
            }catch (LanPingException e){
          
   
                e.printStackTrace();
                computer.reset();
            }catch (SiJiException e){
          
   
                e.printStackTrace();
                throw new ClassingException("老师上课异常");
            }catch (Exception e){
          
   
                e.printStackTrace();
            }
        }
    }
    //电脑类
    static class Computer{
          
   
        private String name;

        //重启电脑
        public void reset(){
          
   
            System.out.println("重启电脑");
        }

        //工作
        //设立一个state,1表示蓝屏,2表示死机
        //声明时可以直接将发生的异常声明,也可以声明他们的父类异常
        public void work(int state)throws LanPingException,SiJiException//Exception
        {
          
   
            if (state == 1){
          
   
                throw new LanPingException(("电脑蓝屏了"));
            }else if (state == 2){
          
   
                throw new SiJiException("电脑死机了");
            }
        }
    }
}

更多Java异常处理,看这篇博文:

经验分享 程序员 微信小程序 职场和发展