异常与处理

常见的运行时异常

  1. NullPointerException 空指针异常
  2. ArithmeticException 数学运算异常
  3. ArrayIndexOutOfBoundsException 数组下标越界异常
  4. ClassCastException 类型转换异常
  5. NumberFormatException 数字格式不正确异常[]

编译异常

  1. SQLException //操作数据库时,查询表可能发生异常
  2. IOException //操作文件时,发生的异常
  3. FileNotFoundException //当操作一个不存在的文件时,发生
  4. 异常ClassNotFoundException //加载类,而该类不存在时,异
  5. 常EOFException //操作文件,到文件末尾,发生异常
  6. lllegalArguementException //参数异常

异常处理

  1. try-catch-finally

    程序员在代码中捕获发生的异常,自行处理

    基本语法

    try{

    ​ //可疑代码

    //将异常生产对应的异常对象,传递给catch块

    //得到异常后,程序员自己处理

    //如果没有发生异常,catch代码块不执行

    }catch(异常){

    //对异常的处理

    }finally{

    ​ //不管try代码块是否有异常发生,始终要执行finally

    ​ //所有,通常将释放资源的代码,放在finally

    }

    • 如果没有出现异常,则执行try块中所有语句,不执行catch块中语句,如果有finally,最后还需要执行finally里面的语句
    • 如果出现异常,则try块中异常发生后,try块剩下的语句不再执行。将执行catch块中的语句,如果有finally,最后还需要执行finally里面的语句!
    /**
    *try-catch快速入门
    */
    public static void main(String[] args) {
        int num1 = 10;
        int num2 = 0;
        try {
        int res = num1 / num2;
        } catch (Exception e) {
        System.out.println(e.getMessage());
        }
    }
    
    /**
    *注意事项
    */
    public class TryCatchDetail {
        public static void main(String[] args) {
    
            try {
            String str = "韩顺平";
            int a = Integer.parseInt(str);
            System.out.println("数字:" + a);
            } catch (NumberFormatException e) {
            System.out.println("异常信息=" + e.getMessage());
            } finally {
            System.out.println("finally 代码块被执行...");
            }
            System.out.println("程序继续...");
        }
    }
    
    public class TryCatchDetail02 {
        public static void main(String[] args) {
    
            try {
            Person person = new Person();
            System.out.println(person.getName());
            int n1 = 10;
            int n2 = 0;
            int res = n1 / n2;
            } catch (NullPointerException e) {
            System.out.println("空指针异常=" + e.getMessage());
            } catch (ArithmeticException e) {
            System.out.println("算术异常=" + e.getMessage());
            } catch (Exception e) {
            System.out.println(e.getMessage());
            } finally {
            }
        }
    }
        class Person {
            private String name = "jack";
            public String getName() {
            return name;
        }
    }
    
    public class TryCatchDetail03 {
        public static void main(String[] args) {
    
            try{
            int n1 = 10;
            int n2 = 0;
            System.out.println(n1 / n2);
            }finally {
            System.out.println("执行了 finally..");
            }
            System.out.println("程序继续执行..");
        }
    }
    
  2. throws

    基本介绍

    1. 如果一个方法(中的语句执行时)可能生成某种异常,但是并不能确定如何处理这种异常,则此方法应显示地声明抛出异常,表明该方法将不对这些异常进行处理,而由该方法的调用者负责处理。
    2. 在方法声明中用throws语句可以声明抛出异常的列表,throws后面的异常类型可以是方法中产生的异常类型,也可以是它的父类

    注意事项

    1. 对于编译异常,程序中必须处理,比如 try-catch 或者 throws
    2. 对于运行时异常,程序中如果没有处理,默认就是throws的方式处理
    3. 子类重写父类的方法时,对抛出异常的规定:子类重写的方法,所抛出的异常类型要
    4. 么和父类抛出的异常一致,要么为父类抛出的异常的类型的子类型
    5. 在throws 过程中,如果有方法 try-catch,就相当于处理异常,就可以不必throws
    /**
    *throws使用
    */
    public class ThrowsDetail {
        public static void main(String[] args) {
            f2();
        }
        public static void f2()  {
    
            int n1 = 10;
            int n2 = 0;
            double res = n1 / n2;
    }
    public static void f1() throws FileNotFoundException {
        f3(); 
    }
    public static void f3() throws FileNotFoundException {
    	FileInputStream fis = new FileInputStream("d://aa.txt");
    }
    public static void f4() {
        f5();
    }
        public static void f5() throws ArithmeticException {
        }
    }
    class Father { 
    	public void method() throws RuntimeException {
    	}
    }
    class Son extends Father {
    @Override
    	public void method() throws ArithmeticException {
    	}
    }
    

自定义异常

  1. 定义:

    当程序中出现了某些“错误”,但该错误信息并没有在Throwable子类中描

    述处理,这个时候可以自己设计异常类,用于描述该错误信息。

  2. 自定义异常的步骤

    • 定义类:自定义异常类名(程序员自己写)继承Exception或RuntimeException
    • 如果继承Exception,属于编译异常
    • 如果继承RuntimeException,属于运行异常(一般来说,继承RuntimeExceptid
    /**
    *自定义异常使用
    */
    public class CustomException {
        public static void main(String[] args)  {
            int age = 180;
            if(!(age >= 18 && age <= 120)) {
                throw new AgeException("年龄需要在 18~120 之间");
            }
            System.out.println("你的年龄范围正确.");
        }
    }
    
    class AgeException extends RuntimeException {
            public AgeException(String message) {
            super(message);
         }
    }
    

throw与throws的区别

意义 位置后面跟的东西
throws 异常处理的一种方式 方法声明处异常类型
throw 手动生成异常对象的关键字 方法体中异常对象
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值