1.try-catch-finally
package example;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
//使用 try-catch-finally 释放资源的模板
public class Example01 {
public static void main(String[] args) {
//声明在外面,如果在try里面声明,finally中访问不到is
InputStream is = null;
try {
is = new FileInputStream("D:\\javaCode\\IOStream\\demo.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
// 在这里判断是否为空,是因为可能在 FileInputStream 实例化之前 就发生了异常
if (is != null) is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
finally在什么时候执行?
package example;
public class Example02 {
public static void main(String[] args) {
try {
System.out.println(10/0);
} catch (Exception e) {
System.out.println("catch 执行了");
} finally {
System.out.println("finally 执行了");
}
}
}
运行结果:
catch 执行了
finally 执行了
结论:
发生异常时,先执行catch,再执行finally
如果try或catch中return了,finally还会执行吗?
package example;
public class Example03 {
public static void main(String[] args) {
System.out.println(chu(10, 0));
}
public static int chu(int a, int b) {
try {
System.out.println("try 执行了");
return a / b;
} catch (Exception e) {
System.out.println("catch 执行了");
return -1;
} finally {
System.out.println("finally 执行了");
return Integer.MAX_VALUE;
}
}
}
运行结果:
try 执行了
finally 执行了
2147483647
结论:
即使try或cantch中使用了return,也会执行finally
package example;
public class Example04 {
public static void main(String[] args) {
System.out.println(chu(10, 0));
}
public static int chu(int a, int b) {
try {
System.out.println("try 执行了");
return a / b;
} catch (Exception e) {
System.out.println("catch 执行了");
return -1;
} finally {
System.out.println("finally 执行了");
}
}
}
运行结果:
try 执行了
catch 执行了
finally 执行了
-1
结论:
如果finally中没有return,那么在执行完finally后,仍然会执行原来代码块中的return
finally一定会执行吗?
package example;
public class Solution06 {
public static void main(String[] args) {
try {
System.out.println("try 执行了");
//结束JVM
System.exit(0);
System.out.println(10/0);
} catch (Exception e) {
System.out.println("catch 执行了");
e.printStackTrace();
} finally {
System.out.println("finally 执行了");
}
}
}
运行结果:
try 执行了
结论:
如果JVM结束了,那么finally也就不会执行了
try-catch-finally的缺点:代码臃肿,不够优雅
2.try-with-resources(jdk7提供)
try(定义资源1;定义资源2;...){
可能出现异常的代码;
}catch(){
异常的处理代码;
}
当资源试使用结束后,会自动调用close方法
package example;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
//使用 try-with-resources 释放资源的模板
public class Example01 {
public static void main(String[] args) {
try (InputStream is = new FileInputStream("D:\\javaCode\\IOStream\\demo.txt")) {
//具体操作
} catch (IOException e) {
e.printStackTrace();
}
}
}
注意!
- try后面的小括号里只能声明资源文件
- 所有资源都实现了AutoCloseable接口,并且都用close方法
模拟一个资源
package example;
public class Resource01 implements AutoCloseable{
@Override
public void close() throws Exception {
System.out.println("释放了 Resource01 的资源");
}
}
package example;
//使用 try-with-resources 释放资源的模板
public class Example01 {
public static void main(String[] args) {
try (Resource01 resource01 = new Resource01()) {
//具体操作
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行结果:
释放了 Resource01 的资源
结论:
自动调用了close()方法关闭资源