java中的finally语句块

java中的finally语句块

1、使用:

try{
	
}catch(){
	
}finally{
	
}
try{
	
}finally{
	
}

2、finally中的代码一定会执行,只有在已执行System.exit(0)退出JVM这一种情况下不会被执行
3、

public class Example {
	public static void main(String[] args) {
		try{
			System.out.println("abc");
			return;
		}finally{
			System.out.println("hhh");
		}
	}
}

执行结果:
在这里插入图片描述
虽然try语句块中有return语句,但是先执行System.out.println(“abc”);再执行finally语句块再执行return
4、

public class Example {
	public static void main(String[] args) {
		try{
			FileInputStream fileInputStream=new FileInputStream("Test.java");//Test.java并不存在
			System.out.println("heihei");
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			System.out.println("hhh");
		}
	}
}

运行结果:
在这里插入图片描述
try…catch()…只能保证编译通过,由于Test.java不存在因此还是会发生运行时异常,e.printStackTrace()输出了异常栈信息,发生异常就不会再继续往下执行System.out.println(“heihei”);但是还是会执行finally语句块输出hhh
5、

public class Example {
	public static void main(String[] args) {
		try{
			System.exit(0);
			System.out.println("heihei");
		}finally{
			System.out.println("hhh");
		}
	}
}

运行结果:
在这里插入图片描述
在执行finally语句块之前已经退出了JVM,不再执行其他代码,也不再执行finally语句块
7、

public class Example {
	public static void main(String[] args) {
		m1();
	}
	public static void m1(){
		try{
			int i=10;
		}finally{
			i++;
			System.out.println(i);
		}
	}
}

运行结果:
在这里插入图片描述
i是在try语句块中声明的,其作用域只在try语句块内,在finally语句块中无法访问 i
8、

public class Example {
	public static void main(String[] args) {
		int m=m1();
		System.out.println("heihei"+m);
	}
	public static int m1(){
		int i=10;
		try{
			return i;
		}finally{
			i++;
			System.out.println("hh"+i);
		}
	}
}

运行结果:
在这里插入图片描述
值得注意的是finally语句块中对i进行的操作并未对return的 i 起作用
原因是代码的执行原理其实是

public class Example {
	public static void main(String[] args) {
		int m=m1();
		System.out.println("heihei"+m);
	}
	public static int m1(){
		int i=10;//放在外面,便于finally语句块访问
		try{
			int temp=i;
			return temp;
		}finally{
			i++;
			System.out.println("hh"+i);
		}
	}
}

JVM将 i 变量临时赋给一个temp变量,把temp变量返回了,把 i 变量加一了
说白了就是try会将变量的值临时储存起来,然后返回这个储存的值
还需要注意的是程序的执行顺序,程序先执行了finally语句,然后才执行return语句

9、由于finally语句块中的代码一定被执行,为了保证某资源一定被释放,通常在finally语句块中释放资源

public class Example {
	public static void main(String[] args) {
		FileInputStream fileInputStream=null;//便于finally语句块访问
		try{
			fileInputStream =new FileInputStream("F:/workspace/hello/src/hello/Hello.java");//Hello.java真实存在
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}finally//为了保证资源释放
		{
			//fileInputStream.close();//流是一个资源,用完时需要关闭
			//由于close方法的声明抛出异常,因此使用close方法时要处理异常
			//public void close() throws IOException{}
			if(fileInputStream!=null)//防止空指针异常
			{
				try{
					fileInputStream.close();
				}catch(IOException e)
				{
					e.printStackTrace();
				}
			}
		}
	}
	
}

运行结果:
在这里插入图片描述
找到了文件并且最后关闭了输入流
10、
final:关键字
finalize:Object类内方法
finally:语句块 异常处理机制中 通常用于释放资源
11、当try语句块中发生异常时,交给catch处理异常,catch处理完整个try{…}catch(…){…}结构就执行结束了,因此不会执行try语句块中的后续代码,但会继续执行try{…}catch(…){…}这个结构后面跟的代码

public class Hello {

	public static void main(String[] args) {
		try{
			FileInputStream fileInputStream=new FileInputStream("hhh.txt");//hhh.txt并不存在
			System.out.println("hahaha");
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}
		
		System.out.println("heihei");
	}
}

运行结果:
在这里插入图片描述
虽然发生了异常,但也输出了heihei

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java ,try-catch-finally 语句用于捕获和处理异常。它的基本语法如下: ```java try { // 可能会抛出异常的代码 } catch (ExceptionType1 e1) { // 处理异常类型1的代码 } catch (ExceptionType2 e2) { // 处理异常类型2的代码 } finally { // 无论是否发生异常,都会执行的代码 } ``` 在 try ,你可以放置可能会抛出异常的代码。如果在执行 try 的代码时发生了异常,那么就会跳到相应的 catch 进行异常处理。catch 可以有多个,每个 catch 用于捕获不同类型的异常,并提供相应的处理逻辑。catch 的参数 e1、e2 是异常对象,你可以使用它们来获取有关异常的信息。 无论是否发生异常,finally 的代码都会被执行。finally 通常用于释放资源或执行清理操作,确保在任何情况下都会执行。 如果在 try 发生了异常,并且没有相应的 catch 来处理该异常,那么该异常将被传递给上层调用栈,直到找到合适的 catch 进行处理,或者如果没有找到任何 catch ,则程序将终止。 以下是一个简单的示例: ```java try { int result = divide(10, 0); System.out.println("结果:" + result); } catch (ArithmeticException e) { System.out.println("除数不能为零!"); } finally { System.out.println("finally 的代码"); } public static int divide(int dividend, int divisor) { return dividend / divisor; } ``` 在上面的示例,divide 方法会抛出一个 ArithmeticException 异常,因为除数为零。在 catch ,我们对该异常进行处理,并输出相应的信息。无论是否发生异常,finally 的代码都会执行,输出 "finally 的代码"。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值