以下内容是从任小龙讲师课堂笔记中整理。
异常处理
如果异常出现的话,会立刻终止程序,所以我们得处理异常:
1.该方法不处理,而是声明抛出,由该方法的调用者来处理(throws).
2.在方法中使用try-catch的语句块来处理异常
使用try-catch处理异常:
try{
//TODO 编写可能会出现异常的代码
}catch(异常类型 e){
//处理抛出的异常
//记录日志//打印异常信息//继续抛出异常
}
正常和异常的表现:
如何处理异常呢?
异常分支处理流程:
注意:try和catch都不能单独使用,必须联合使用
获取异常信息
如何获取异常信息,Throwable类的方法:
1.String getMessage():获取异常的描述信息
2.String toString():获取异常的类型和异常描述信息(不用)
3.void printStackTrace():打印异常的跟踪栈信息,并输出到控制台。不需要使用System.out.print
包含了异常的类型,异常的原因,还包括异常出现的位置,在开发和调试阶段都的使用printStackTrace,运行阶段不建议使用,因为影响性能。
try-catch捕获多个异常
try{
//编写可能会出现异常的代码
}catch(异常类型A e){
//处理抛出的异常:记录日志//打印异常信息//继续抛出异常
}catch(异常类型B e){
//处理抛出的异常:记录日志//打印异常信息//继续抛出异常
}catch(Exception e){
//如果是其他异常,可以使用Exception超类接收其他异常
}
注意:
1.一个catch语句,只能捕获一种类型的异常,如果需要捕获多种异常,就得使用多个catch语句。
2.代码在一瞬间只能出现一种类型的异常,只需要一个catch捕获,不可能同时出现多个异常。
public class test{ public static void main(String[] args){ System.out.println("begin ..."); String sNum1 = "10"; String sNum2 = "2B"; try { //把String转换为int类型 int num1 = Integer.parseInt(sNum1); int num2 = Integer.parseInt(sNum2); int ret = num1 / num2; System.out.println(ret); }catch(ArithmeticException e){ System.out.println("除数为0"); }catch(NumberFormatException e){ System.out.println("类型转换异常"); }catch(Exception e){ //如果是其他异常,可以使用Exception超类接收其他异常 System.out.println("未知异常"); } System.out.println("end ..."); } }
finally
finally语句块表示最终都会执行的代码,无论有没有异常
什么时候的代码必须最终执行:
当我们在try语句块中打开了一些物理资源(磁盘文件/网络连接/数据库连接等),在使用完毕之后,最终关闭打开的资源。
finally的两种语法:
1.try…finally:此时没有catch来捕获异常,因为此时根据应用场景,我们会抛出异常,自己不处理。
2.try…catch…finally:自身需要处理异常,还得关闭资源
注意:finally不能单独使用
当只有在try或catch中调用JVM的相关方法,否则finally永远会执行。
如果finally有return语句,永远返回finally中的结果,避免该情况。
public class test{ public static void main(String[] args){ System.out.println("begin ..."); String sNum1 = "10"; String sNum2 = "2B"; try { //把String转换为int类型 int num1 = Integer.parseInt(sNum1); int num2 = Integer.parseInt(sNum2); int ret = num1 / num2; System.out.println(ret); }catch(ArithmeticException e){ System.out.println("除数为0"); }catch(NumberFormatException e){ System.out.println("类型转换异常"); }catch(Exception e){ //如果是未知异常,可以使用Exception超类接收其他异常 System.out.println("未知异常"); try { throw e; } catch (Exception e1) { e1.printStackTrace(); } }finally{ System.out.println("关闭资源"); } System.out.println("end ..."); } }
异常(Exception)的分类:根据在编译时期还是运行时期去检查异常?
1.编译时期异常:checked异常,在编译时期就会检查,如果没有处理异常,则编译失败
2.运行时期异常:runtime异常,在运行时期,运行异常不会编译器检测(不报错)。
运行异常:在编译时期可处理,可不处理
如何确定某一个异常是编译异常还是运行异常?
Exception子类中除RuntimeException及其子类是运行时期异常,其他全是编译异常。
throw
throw语句:
运用于方法内部,抛出一个具体的异常对象。
throw new 异常类("异常信息");
一般的当一个方法出现不正常的情况的时候,我们不知道该方法应该返回什么,此时就返回一个错误,在catch语句块中继续向上抛出异常。
return是返回一个值,throw是返回一个错误,返回给该方法的调用者。
示例
public class test{ public static void main(String[] args){ try { int ret = divide(10, 0); System.out.println(ret); }catch(Exception e){ System.out.println(e.getMessage()); } } private static int divide(int num1, int num2){ System.out.println("begin ..."); try { int ret = num1 / num2; System.out.println(ret); }catch(ArithmeticException e){ System.out.println("throw exception"); throw new ArithmeticException("除数为0"); }finally{ System.out.println("关闭资源"); } System.out.println("end ..."); return 0; } }
运行结果
begin ...
throw exception
关闭资源
除数为0
throws
throws用于方法声明之上,用于表示当前方法不处理异常,而是提醒该方法的调用者来处理异常。
如:private static int divide(int num1, int num2) throws Exception
如果每个方法都放弃处理异常,直接通过throws声明抛出,最后异常会抛到main方法,如果此时main方法不处理,继续抛出给JVM,底层处理机制就是打印异常的跟踪栈信息。
runtime异常默认就是这种处理方式。
示例:
public class test{ public static void main(String[] args){ try { int ret = divide(10, 0); System.out.println(ret); }catch(Exception e){ System.out.println(e.getMessage()); } } private static int divide(int num1, int num2) throws Exception{ System.out.println("begin ..."); try { int ret = num1 / num2; System.out.println(ret); }catch(ArithmeticException e){ System.out.println("throw exception"); throw new ArithmeticException("除数为0"); }finally{ System.out.println("关闭资源"); } System.out.println("end ..."); return 0; } }
自定义异常类
为什么需要自定义异常类:
Java中不同的异常类分别表示着某一种具体的异常情况,那么在开发中总是有些异常情况是SUN没有定义好的,此时我们根据自己业务的异常情况来定义异常类。
什么是自定义异常类:在开发中根据自己业务的异常情况来定义异常类,自定义一个业务逻辑异常类:LogicException
异常类如何定义:
方式1.自定义一个受检查的异常类:自定义类并继承于java.lang.Exception
方式2.自定义一个运行时期的异常类,自定义类并继承于java.lang.RuntimeException.
示例
public class LogicException extends Exception { public LogicException() { } public LogicException(String message) { super(message); } public LogicException(String message, Throwable cause) { super(message, cause); } public LogicException(Throwable cause) { super(cause); } public LogicException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } }