其他分享
首页 > 其他分享> > 异常(课堂笔记3)

异常(课堂笔记3)

作者:互联网

1.异常:程序在运行的过程中出现的例外,会中断正在运行的程序。异常不是错误。

public class Dome {
    public static void main(String[] args) {
        try {
            //会出现异常的代码块
        }catch (Exception e){
            //处理异常
            System.out.println(e.getMessage());//打印异常原因
        }finally {
            //无论代码是否异常都会运行,一般存放关闭资源的代码。
        }
    }
}

2.自定义异常:throws,throw。

public class Dome {
//throws谁使用谁处理异常
public static void main(String[] args)throws ArithmeticException {
//throw引出异常
       throw new ArithmeticException("算数异常");
    }
}

常见异常:

Exception所有异常的父类

1.ArithmeticException                算术异常
 2.ArrayIndexOutOfBoundsException        数组下标越界异常
 3.NullPointerException                空指针异常
  4.ClassCastException                类型转换异常        
  5.NumberFormatException                数字格式化异常
  6.IllegalArgumentException            参数不匹配异常
 

标签:throws,ArithmeticException,笔记,throw,static,课堂,异常,public
来源: https://blog.csdn.net/qq_57336374/article/details/120513109