目录
程序中的异常
异常的定义
本质
常见的异常
异常的分类
异常的处理
运行时异常
错误时异常
编译时处理异常
throws作用
throw关键字
与throws的区别
finally关键字
语法规则
final finally finalize 的区别
自定义异常类
示例 编写一个分数必须在0~100之间的异常
程序中的异常
比如做除法时,除数为0,或引用类型未指定具体实例,就会出现异常
import java.util.Arrays; public class exception {public static void main(String[] args) {int i = 10;System.out.println(i/0); // java.lang.ArithmeticException String str = null; System.out.println(str.length()); // java.lang.NullPointerException} }
异常的定义
异常指的是程序的运行过程中发生的不正常的事件,它会中断正在运行的程序,即程序出现了不正常的情况。
本质
Java中对可能出现的问题进行描述的一种对象体现
常见的异常
-
除数不能为0 ArithmeticException
-
空指针异常 NullPointEception
-
数组下标越界 ArrayIndexOutBoundsException
-
类型转换异常 ClassCastException
异常的分类
-
编译异常【Exception】
-
运行时异常【RuntimeException】
-
错误【Error】
异常的处理
运行时异常
-
自己处理异常 -- 通过try catch 语句块处理
-
单个异常处理
-
语法规则
try{// 放置可能出现问题的代码 }catch (异常类型 异常名称){// 放置处理异常的代码 }finally {// 释放资源 }
import java.util.Arrays; public class exception {public static void main(String[] args) {System.out.println("一");int i = 10;try{System.out.println(i / 0); //java.lang.ArithmeticException}catch (ArithmeticException e){// catch捕获声明的异常应该与实际抛出的异常类型一致,要么就是存在继承关系,才能捕获到,eg: catch (Exception e)System.out.println("除数为0");} //Java处理异常比较武断,会直接终止程序,不往后执行System.out.println("二");} }
-
-
多个异常处理
-
语法规则
try{// 放置可能出现问题的代码 }catch (子异常类型 异常名称){// 放置处理异常的代码 }catch (子异常类型 异常名称){...............}catch (Exception e){e.printStackTrace(); // 打印详细的异常信息,根据异常错误信息找bug }finally {// 释放资源 }
-
import java.util.Arrays; public class exception {public static void main(String[] args) {System.out.println("一");int i = 10;String str = null;int[] array = new int[3];// try块中,处理时,是从上往下执行的,只要匹配到了对应的异常,就不会执行后面的异常,就会跳出try块,catch中也是try{System.out.println(i / 0);System.out.println(str.length());array[10] = 100;}catch (ArithmeticException e){System.out.println("除数为0");}catch (NullPointerException e){System.out.println("空指针异常");}catch (ArrayIndexOutOfBoundsException e){System.out.println("数组下标越界");}System.out.println("二");} }
import java.util.Arrays; public class exception {public static void main(String[] args) {System.out.println("一");int i = 10;String str = null;int[] array = new int[3];// 处理时,只要匹配到了异常,就不会执行后面的异常try{System.out.println(i / 0);System.out.println(str.length());array[10] = 100;}catch (ArithmeticException e){System.out.println("除数为0");}catch (NullPointerException e){System.out.println("空指针异常");}catch (ArrayIndexOutOfBoundsException e){System.out.println("数组下标越界");}catch (Exception e){e.printStackTrace(); // 如果以上的异常都没匹配到,最终会通过Exception匹配到}System.out.println("二");} }
-
注意:异常的父类必须写子类后面,否则不会执行到子类
错误时异常
无法处理
编译时处理异常
throws作用
定义一个方法时使用throws关键字声明,使用throws关键字声明的方法表示此方法不处理异常,交给调用此方法者处理
-
将异常抛出(throws 关键字)
-
语法规则
[修饰符] 返回值类型 方法名(参数列表) [throws 异常类1,异常类2,.....]{ }
注意:
-
调用的方法如果有throws编译时异常,在调用时必须要处理
-
重写一个方法时,所声明的异常范围不能被扩大(例如父类是一个具体异常,子类是Exception,就会出错)
-
package EXce ptionLearn; import java.io.FilterOutputStream; import java.text.ParseException; import java.text.SimpleDateFormat; public class ThrowsLearn {public static void main(String[] args) throws ParseException {try{calc();}catch (ArithmeticException e){System.out.println("除数为0");}System.out.println("*****************");} // throws ArithmeticException 谁调用本方法,谁可能抛出异常public static int calc() throws ArithmeticException{int a = 10;int b = 0;int result = a / b;return result;} }
throw关键字
package EXceptionLearn;import java.text.ParseException;public class qubie {public static void main(String[] args) {try{calc();}catch (ArithmeticException e){System.out.println("除数不能为0哦!"); // e.printStackTrace();}try{show();}catch (ParseException e){System.out.println("转换异常");}}public static int calc() throws ArithmeticException{int a = 10;int b = 0;if(b == 0){throw new ArithmeticException("除数为0,无法运算");} // 与 a / b 会抛出的异常一致int result = a / b;return result;}static void show() throws ParseException {int a = 33;if(a == 33){throw new ParseException("转换异常",1);}} }
与throws的区别
-
throws用在方法名后,跟的是异常名,throw用在方法体中,跟的异常对象
-
throws可以跟多个异常名,用逗号隔开,throw只能抛出一个异常对象
-
throws表示抛出异常,由该方法的调用者处理,throw表示抛出异常,由方法体内的语句处理
-
throws表示出现异常的一种可能性,异常并不一定发生,throw是抛出了具体的异常,真实的产生了Exception对象
finally关键字
语法规则
try{// 放置可能出现问题的代码 }catch (异常类型 异常名称){// 放置处理异常的代码 }finally {// 释放资源 }
进入了try块中,finally修饰的代码一定会执行(无论抛不抛出异常),除非在执行finally之前程序异常退出或调用了系统的退出方法(System.exit())或return
package EXceptionLearn;public class FinallyLearn {public static void main(String[] args) {int a = 10;int b = 0;try {System.out.println(a / b);}catch (Exception e){System.out.println("除数不能为0!!");}finally {System.out.println("结束 .....");}System.out.println("***********");} }// 除数不能为0!! // 结束 ..... // ***********
public class Test {public static void main(String[] args) {System.out.println(test());}public static int test(){int x = 1;try {x++;return x; // 2}finally {// finally 中改变不了结果的值++x;System.out.println(x); // 3}} }// 输出: // 3 // 结果2
final finally finalize 的区别
-
final 修饰的类不能被继承,修饰的方法不能被重写,修饰的变量变常量
-
finally 是try catch语句中的一部分,表示最终执行的代码,常用来做资释放
-
finalize 是 Object中的方法,在垃圾回收中会用到
自定义异常类
Java中的异常都是Throwable、Exception、RuntimeException的子类,要创建一个自定义的类,其实就是创建其对应的子类
示例 编写一个分数必须在0~100之间的异常
public class Plearn extends Exception{public Plearn(){}public Plearn(String message){super(message);} }
public class Plearn extends Exception{public Plearn(){}public Plearn(String message){super(message);}public static void main(String[] args) {try {System.out.println(getScore());}catch (Exception e){e.printStackTrace();}System.out.println("************");}public static double getScore() throws Plearn {double score = 999;if(score < 0 || score > 100){throw new Plearn("分数不合法");}return score;} }