Java异常的处理
1.不对产生异常的代码进行处理,而是声明抛出(throws),交给调用者来处理,如果调用者也没有处理,则交给JVM处理,JVM通过终止程序并打印异常类型来告诉我们异常产生的原因和位置。
2.使用try-catch来处理异常,捕获异常,并对异常进行针对性的处理。
try-catch的方式就是捕获异常
- 捕获异常:Java中对异常有针对性的语句进行捕获,可以对出现的异常进行指定方式的处理。
捕获异常语法如下:
...
try{
...
//可能出现的异常代码
} catch(异常类型 异常对象名) {
...
//处理异常的代码
}
...
Tips:try和catch都不可以单独使用
finally代码块
- 当有一些特定的代码无论异常是否发生,都需要执行时,可以将这样的代码放在finally代码块中,凡是在finally代码块中存放的代码在一般情况下都会执行。
- 什么时候的代码必须最终执行?
- 当我们在try语句块中打开了一些物理资源(磁盘文件/网络连接/数据库连接等),我们都得在使用完之后,最终关闭打开的资源。
finally的语法
...
try{
InputStream is = new FileInputStream("text");
is.read();
}catch(Exception e){
...
}finally{
is.close();
//一定要执行的代码
}
Tips:
1.finally不可以单独使用
2.只有在try或者catch中调用退出JVM的相关方法(System.exit(0)),此时finally才不会执行,否则finally永远会执行下去
Java7新的异常处理机制–try-with-resource
- 当我们在try-catch中编写了打开系统资源的语法时,必须在finally代码块里面编写关闭资源的语法,如果没有在finally代码块中释放资源,就会造成系统资源浪费。
- 而
Java7
中提供了一个新的异常处理机制try-with-resource
。它可以很容易地关闭在try-catch
中使用的资源。try-with-resource
确保了每个资源在语句结束的时候被关闭。
try-with-resouce用法
...
try(InputStream is = new FileInputStream("demo")){
is.read();
//try后面的()内完成了资源的初始化
} catch(Exception e){
...
} finally {
//is.close();
//可以不写关闭资源的代码
}
...
Tips:这种用法可以省略catch和finally部分
try-with-resource和AutoCloseable接口
- 并不是所有的类都可以使用try-with-resource处理。实现AutoCloseable接口是使用try-with-resource的前提。在try-with-resource语句中,只有实现了AutoCloseable接口的类才会被视为资源进行相关处理。(或者实现AutoCloseable的子接口Closeable)
- close方法是AutoCloseable接口的唯一方法,该方法在程序运行时会被自动调用。
- 在一个try语句中可以声明多个资源类。
- try中的多个资源在初始化过程中,一旦发生异常,那些已经完成初始化的资源,将按照与其创建时相反的顺序依次关闭。
- 若try中用到了多个资源类,则close方法会按照与其声明时相反的顺序依次调用。
- try-with-resources也可以与catch和finally关键字连用,功能与之前一样。
- 如果在try-with-resources语句中遇到了异常,close关闭语句会先于catch语句执行。
- 实现AutoCloseable接口时,最佳做法是抛出一个具体的异常,而不是抛出最上级的Exception自身。这一点需要引起重视,因为AutoCloseable接口的close方法签名抛出的就是Exception异常。
public interface AutoCloseable {
void close() throws Exception;
}
...
public interface Closeable extends AutoCloseable{
public void close() throws IOException;
}
- try-with-resources中的资源会被隐式地声明为final
自定义类实现AutoCloseable接口并测试
//Lion类
public class Lion implements AutoCloseable {
public Lion(){
System.out.println("LION is OPEN in the wild!");
}
public void hunt() throws Exception {
throws new Exception ("DeerNotFound says Lion!");
}
public void close() throws Exception {
System.out.println("LION is CLOSED in the cage.");
throw new Exception("Unable to close the cage!");
}
}
//Tiger类
public class Tiger implements AutoCloseable {
publci Tiger(){
System.out.println("TIGER is OPEN in the wild!");
}
public void hunt() throws Exception {
throw new Exception ("DeerNotFound says Tiger!");
}
public void close() throws Exception {
System.out.println("TIGER is CLOSED in the cage.");
}
}
//测试类
public class TryWithResourceTest {
public static void main(String[] args) {
try(Lion lion = new Lion();Tiger tiger = new Tiger()) {
lion.hurt();
tuger.hunt();
} catch (Exception e) {
System.out.println(e);
} finally {
System.out.println("Finally!");
}
}
}
//运行结果
LION is OPEN in the wild!
TIGER is OPEN in the wild!
TIGER is CLOSED in the cage!
LION is CLOSED in the cage!
java.lang.Exception:DeerNotFound says Lion!
Finally!
分析