一、Exception
java.long
作用:
1、 向用户通知错误,显示异常现象,并处理错误,可以执行后续程序
2、让程序更加安全健壮,以妥善的方式终止程序
例如:密码输入错误、用户名不正确、格式不正确等等
3、Throwable
两个子类:Exception和Error
常用方法
toString 、getMessage 、printstackTrac
//这里是三个异常处理的方法,就是打印异常信息
System.out.println(e.getMessage());//文件后缀名不正确
System.out.println(e.toString());//java.io.IOException: 文件后缀名不正确
e.printStackTrace();
/*
java.io.FileNotFoundException: 文件后缀名不正确
at damo1.tryCatchException.readFile(tryCatchException.java:37)
at damo1.tryCatchException.main(tryCatchException.java:27)~
*/
4、异常的分类
CheckedException是编译期异常,要自己处理,使用throws 或者 tryt{}catch{}处理
RunTimeException是运行期异常,不需要处理,交给JVM处理
5、常见的异常
java.lang.NullpointerException(空指针异常)
java.lang.ClassNotFoundException(指定的类不存在)
java.lang.ClassNotFoundExceptio(指定的类不存在)
java.lang.IndexOutOfBoundsException(数组下标越界异常)
java.lang.IllegalArgumentException(方法的参数错误)
java.lang.IllegalAccessException(没有访问权限)
java.lang.ArithmeticException(数学运算异常)
java.lang.ClassCastException(数据类型转换异常)
java.lang.FileNotFoundException(文件未找到异常)
java.lang.ArrayStoreException(数组存储异常)
java.lang.NoSuchMethodException(方法不存在异常)
java.lang.EOFException(文件已结束异常)
java.lang.InstantiationException(实例化异常)
java.lang.InterruptedException(被中止异常)
java.lang.CloneNotSupportedException (不支持克隆异常)
java.lang.OutOfMemoryException (内存不足错误)
java.lang.NoClassDefFoundException (未找到类定义错误)
6、异常的处理方法
关键字:
抛出异常:throw
声明异常:throws
捕获异常:try、catch
finally代码块
(1)throws 与 throw格式:
修饰符列表 返回值类型 方法名(参数列表) throws A异常,B异常… {
throw new A异常(“里面可以写内容’);
throw new B异常(“里面可以写内容’);
…
}
throws方法自己不能处理以异常,直接中断程序,交给JVM处理,不能执行后续代码。
public static void main(String[] args) throws FileNotFoundException,IOException{~
readFile("c:\\a.txt");~
}
public static void readFile(String FileName) throws FileNotFoundException,IOException{
//public static void readFile(String FileName) throws Exception{
if(!FileName.equals("c:\\a.java")){
throw new FileNotFoundException("没有找到文件位置");
}
if (!FileName.endsWith(".java")){
throw new IOException("文件后缀名有误!");
}
System.out.println("文件访问路径正确,正在读取文件!");
}~
(2)、try 和 catch格式
try{
可能发生异常的代码
}catch (异常 变量名){
处理的代码
}
try和catch是自己来处理异常,可以执行后续的代码
多个异常,多个try,多个catch
多个异常,一个try,多个catch
一个try多个catch注意事项:
先写子类异常,再先父类异常
例如: ArrayIndexOutOfBoundsException extends IndexOutOfBoundsException
多个异常,一个try,一个catch
public static void main(String[] args) {
int arr[] = {2, 3, 5};
ArrayList<Integer> list = new ArrayList<>();
Collections.addAll(list, 20, 30, 40);
//多个异常,多个try,多个catch
try {
System.out.println(arr[3]);
} catch (ArrayIndexOutOfBoundsException E) {
System.out.println(E);
}
try {
System.out.println(list.get(3));
}catch (IndexOutOfBoundsException e){
System.out.println(e);
}
//多个异常,一个try,多个catch
try {
System.out.println(arr[3]);
System.out.println(list.get(3));
//先写子类的异常
} catch (ArrayIndexOutOfBoundsException E) {
System.out.println(E);
} catch (IndexOutOfBoundsException e) {
System.out.println(e);
}
//多个异常,一个try,一个catch,就是使用父类异常
try {
System.out.println(arr[3]);
System.out.println(list.get(3));
} catch (Exception E) {
System.out.println(E);
}
}~
public static void main(String[] args) {
try {
readFile("c:\\a.txt");//可能存在异常问题的代码
} catch (IOException e) { //异常 变量名
// e.printStackTrace(); //这里只详细打印了异常信息
//这里是三个异常处理的方法,就是打印异常信息
System.out.println(e.getMessage());//文件后缀名不正确
System.out.println(e.toString());//java.io.IOException: 文件后缀名不正确
e.printStackTrace();
//后面是自己处理异常的代码
System.out.println("自己处理异常的代码");
System.out.println("文件后缀名错误");
}
System.out.println("后面执行的代码");~
(3)、finally
和 try 一起使用
作用:
不管程序有无异常,都可以执行后面程序,也叫资源释放(资源回收)
try{
}catch (){
}finally{
后续程序
}
7、自定义异常类
格式:
public classi 自定义异常类 extends Exceptio 或RuntimeException{
一个空参构造方法
一个有异常参数信息的构造方法
}
public class ZiDingYiException extends Exception /* RuntimeException*/{
public ZiDingYiException (){
super();
}
public ZiDingYiException(String exception) {
super(exception);//exception是一个变量名
}~