java异常分类:
Throwable又派生出Error类和Exception类。
错误(Error):Error类以及他的子类的实例,代表了JVM本身的错误。错误不能被程序员通过代码处理,Error很少出现。eg:Virtual MachineError(虚拟机错误)、NoClassDefFoundError(类定义错误)
异常(Exception):Exception以及他的子类,代表程序运行时发送的各种不期望发生的事件。可以被Java异常处理机制使用,是异常处理的核心。
Checked Exception:所有checked exception都是需要在代码中(使用try…catch…finally或者throws)处理的。它们的发生是可以预测的。比如IOException,除了RuntimeException及其子类以外,都是checked exception
Unchecked Exception:RuntimeException及其子类都unchecked exception。比如空指针异常,算数异常等,这种异常是运行时发生,无法预先捕捉处理的。Error也是unchecked exception,也是无法预先处理的。
try-catch-finally语句
(1)try块:负责捕获异常,一旦try中发现异常,程序的控制权将被移交给catch块中的异常处理程序。
【try语句块不可以独立存在,必须与 catch 或者 finally 块同存】
(2)catch块:发出警告:提示、检查配置、网络连接,记录错误等。执行完catch块之后程序跳出catch块,继续执行后面的代码。
【多个catch块处理的异常类,要按照先catch子类后catch父类的处理方式,因为会【就近处理】异常(由上自下)】
(3)finally:最终执行的代码,用于关闭和释放资源。
结构:
try{
}catch(){
}
try{
}catch(){
}catch(){
return ;
}finally{
//一定会执行的代码
}
try{
//
}finally{
//一定会执行的代码
}
注意:
1、try块中的局部变量和catch块中的局部变量(包括异常变量),以及finally中的局部变量,他们之间不可共享使用。
2、每一个catch块用于处理一个异常。异常匹配是按照catch块的顺序从上往下寻找的,只有第一个匹配的catch会得到执行。匹配时,不仅运行精确匹配,也支持父类匹配,因此,如果同一个try块下的多个catch异常类型有父子关系,应该将子类异常放在前面,父类异常放在后面,这样保证每个catch块都有存在的意义。
3、java中,异常处理的任务就是将执行控制流从异常发生的地方转移到能够处理这种异常的地方去。也就是说:当一个函数的某条语句发生异常时,这条语句的后面的语句不会再执行,它失去了焦点。执行流跳转到最近的匹配的异常处理catch代码块去执行,异常被处理完后,执行流会接着在“处理了这个异常的catch代码块”后面接着执行。
有的编程语言当异常被处理后,控制流会恢复到异常抛出点接着执行,这种策略叫做:resumption model of exception handling(恢复式异常处理模式 )
而Java则是让执行流恢复到处理了异常的catch块后接着执行,这种策略叫做:termination model of exception handling(终结式异常处理模式)
finally
finally块不管异常是否发生,只要对应的try执行了,则它一定也执行。只有一种方法让finally块不执行:System.exit()。因此finally块通常用来做资源释放操作:关闭文件,关闭数据库连接等等。
良好的编程习惯是:在try块中打开资源,在finally块中清理释放这些资源。
注意:
1、finally块没有处理异常的能力。处理异常的只能是catch块。
2、在同一try…catch…finally块中 ,如果try中抛出异常,且有匹配的catch块,则先执行catch块,再执行finally块。如果没有catch块匹配,则先执行finally,然后去外面的调用者中寻找合适的catch块。
3、在同一try…catch…finally块中 ,try发生异常,且匹配的catch块中处理异常时也抛出异常,那么后面的finally也会执行:首先执行finally块,然后去外围调用者中寻找合适的catch块。
throw
程序员也可以通过throw语句手动显式的抛出一个异常。throw语句的后面必须是一个异常对象。
throw 语句必须写在函数中,执行throw 语句的地方就是一个异常抛出点,它和由JRE自动形成的异常抛出点没有任何差别。
public void save(User user){
if(user == null)
throw new IllegalArgumentException("User对象为空");
//......
}
throws
如果一个方法内部的代码会抛出检查异常(checked exception),而方法自己又没有完全处理掉,则javac保证你必须在方法的签名上使用throws关键字声明这些可能抛出的异常,否则编译不通过。throws仅仅是将函数中可能出现的异常向调用者声明,而自己则不具体处理。
public void foo() throws ExceptionType1 , ExceptionType2 ,ExceptionTypeN{
//可能发生异常 ExceptionType1 , ExceptionType2 ,ExceptionTypeN
}
自定义异常
自定义的异常应该总是包含如下的构造函数:
-
一个无参构造函数
-
一个带有String参数的构造函数,并传递给父类的构造函数。
-
一个带有String参数和Throwable参数,并都传递给父类构造函数。
-
一个带有Throwable参数的构造函数,并传递给父类的构造函数。
public class BussinessException extends Exception { private int errorCode; private static final long serialVersionUID = 8712211240420456880L; public BussinessException() { super(); } public BussinessException(String message) { super(message); } public BussinessException(int errorCode, String message) { super(message); this.errorCode = errorCode; } public BussinessException(int errorCode, String message, Throwable cause) { super(message, cause); this.errorCode = errorCode; } public int getCode() { return errorCode; } public void setCode(final int errorCode) { this.errorCode = errorCode; } }
注意:
1.当子类重写父类的带有 throws声明的函数时,其throws声明的异常必须在父类异常的可控范围内——用于处理父类的throws方法的异常处理器,必须也适用于子类的这个带throws方法 。这是为了支持多态。
例如,父类方法throws 的是2个异常,子类就不能throws 3个及以上的异常。父类throws IOException,子类就必须throws IOException或者IOException的子类。
2.Java程序可以是多线程的。每一个线程都是一个独立的执行流,独立的函数调用栈。如果程序只有一个线程,那么没有被任何代码处理的异常 会导致程序终止。如果是多线程的,那么没有被任何代码处理的异常仅仅会导致异常所在的线程结束。
finally中的return 会覆盖 try 或者catch中的返回值。
public static void main(String[] args) {
int result;
result = foo();
System.out.println(result); /2
result = bar();
System.out.println(result); /2
}
@SuppressWarnings("finally")
public static int foo() {
trz{
int a = 5 / 0;
} catch (Exception e){
return 1;
} finally{
return 2;
}
}
@SuppressWarnings("finally")
public static int bar() {
try {
return 1;
}finally {
return 2;
}
}
finally中的return会抑制(消灭)前面try或者catch块中的异常
class TestException{
public static void main(String[] args) {
int result;
try{
result = foo();
System.out.println(result); //输出100
} catch (Exception e){
System.out.println(e.getMessage()); //没有捕获到异常
}
try{
result = bar();
System.out.println(result); //输出100
} catch (Exception e){
System.out.println(e.getMessage()); //没有捕获到异常
}
}
//catch中的异常被抑制
@SuppressWarnings("finally")
public static int foo() throws Exception {
try {
int a = 5/0;
return 1;
}catch(ArithmeticException amExp) {
throw new Exception("我将被忽略,因为下面的finally中使用了return");
}finally {
return 100;
}
}
//try中的异常被抑制
@SuppressWarnings("finally")
public static int bar() throws Exception {
try {
int a = 5/0;
return 1;
}finally {
return 100;
}
}
}
== finally中的异常会覆盖(消灭)前面try或者catch中的异常==
class TestException{
public static void main(String[] args) {
int result;
try{
result = foo();
} catch (Exception e){
System.out.println(e.getMessage()); //输出:我是finaly中的Exception
}
try{
result = bar();
} catch (Exception e){
System.out.println(e.getMessage()); //输出:我是finaly中的Exception
}
}
//catch中的异常被抑制
@SuppressWarnings("finally")
public static int foo() throws Exception {
try {
int a = 5/0;
return 1;
}catch(ArithmeticException amExp) {
throw new Exception("我将被忽略,因为下面的finally中抛出了新的异常");
}finally {
throw new Exception("我是finaly中的Exception");
}
}
//try中的异常被抑制
@SuppressWarnings("finally")
public static int bar() throws Exception {
try {
int a = 5/0;
return 1;
}finally {
throw new Exception("我是finaly中的Exception");
}
}
}
注意:
- 不要在fianlly中使用return。
- 不要在finally中抛出异常。
- 减轻finally的任务,不要在finally中做一些其它的事情,finally块仅仅用来释放资源是最合适的。
- 将尽量将所有的return写在函数的最后面,而不是try … catch … finally中。