JAVA学习24---异常

异常的概念

在Java中,将程序执行过程中发生的不正常情况称为“异常”。

异常的分类

  1. Error:Java虚拟机无法解决的严重问题。如:JVM系统内部的错误,资源耗尽等严重情况。一般不编写针对性的代码进行处理。
  2. Exception:其他因为编程错误或偶然的外在因素导致的一般性问题,可以使用针对性的代码进行处理。如:空指针访问、试图读取不存在的文件、网络连接中断、数组下标越界等。

解决的方案

一种方法是遇到错误就终止程序的运行,另一种方法是程序员在编写程序时,就考虑到错误的检测、错误消息的提示,以及错误的处理。

常见的异常

  • NullPointerException
public class demo {
    public static void main(String[] args) {
        String a ="abc";
        a = null;
        System.out.println(a.charAt(3));
    }
}
  • ArrayIndexOutOfBoundsException
public class demo {
    public static void main(String[] args) {
        int[] a = new int[3];
        System.out.println(a[3]);
    }
}
  • ClassCastException
public class demo {
    public static void main(String[] args) {
        Object a = new Date();
        String b = (String)a;
    }
}
  • NumberFormatException
public class demo {
    public static void main(String[] args) {
        String a = "abc";
        int b = Integer.parseInt(a);
    }
}
  • InputMismatchException
public class demo {
    public static void main(String[] args) {
        Scanner a = new Scanner(System.in);
        int b = a.nextInt();
        System.out.println(b);
    }
}
  • ArithmeticException
public class demo {
    public static void main(String[] args) {
        int a = 1;
        int b = 0;
        System.out.println(a/b);
    }
}

异常的处理

  • try-catch-finally

格式:

        try{
            //可能出现异常的代码
        }catch (异常类型1 变量名1){
            //处理异常的方式
        }catch (异常类型2 变量名2){
            //处理异常的方式
        }catch (异常类型3 变量名3){
            //处理异常的方式
        }
        finally {
            //最后执行的代码
        }

try-catch例子:

public class demo {
    public static void main(String[] args) {
        String str = "123";
        str = "abc";
        try {
            int num = Integer.parseInt(str);
        }catch (NumberFormatException a){
            System.out.println("数值转换异常!");
            //以下为2个常用的异常显示方式
            System.out.println(a.getMessage());
            
            a.printStackTrace();
        }catch (NullPointerException a){
            System.out.println("空指针异常!");
        }catch (Exception a){
            System.out.println("异常!");
        }
    }
}

try-catch-finally例子:

    public int method() {
        int a = 10;
        int b = 0;
        try {
            System.out.println(a / b);
            return 1;
        }catch (ArithmeticException e){
            System.out.println("除0错误!");
            return 0;
        }finally {
            System.out.println("一定执行");
            return 2;//最后返回值为2!
        }
    }

注意:
1.finally是可选的
2.使用try将可能出现异常的代码包装起来,在执行的过程中,一旦出现异常,就会生成一个对应异常的对象,根据此对象的类型,去catch中进行匹配
3.一旦try中的异常匹配到某一个catch时,就进入此catch的异常处理,处理完成后就跳出try-catch结构,执行后续代码
4.catch中异常类型如果没有子父类关系,则无先后声明顺序,如果有子父类关系,则子类一定要声明在父类上面

  • throws

格式:
throws + 异常类型
例子:

public class demo {
    public static void main(String[] args) {
        try {
            method2();
        }catch (Exception e){
            System.out.println("问题抛出到main方法中解决!");
        }
    }
    
    public static void method2()throws Exception{
        method1();
    }

    public static int method1() throws ArithmeticException{
        int a = 10;
        int b = 0;
        return a/b;
    }
}

注意:
1.“throws + 异常类型”写在方法的声明处,指明此方法执行时可能会抛出的异常类型
2.一旦当方法执行时,出现异常,就会在异常代码处生成一个异常类的对象,此对象若满足throws后异常类型时,就会抛出,异常代码的后续代码,就不再执行

手动抛出异常

throw方法:

public class demo {
    public static void main(String[] args) {
        Student s = new Student();
        try {
            s.show(-1);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }
static class Student{
        private int id;
        public void show(int id) throws Exception{
            if(id>=0){
                this.id=id;
            }else{
                throw new Exception("输入非法");//手动抛出异常对象
            }
        }
}

}

自定义异常(略)

  1. 继承于现有的异常结构:RuntimeException、Exception
  2. 提供全局变量:serialVersionUID
  3. 提供重载的构造器
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值