Java中释放资源的两种方式

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()方法关闭资源
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迪迦敲代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值