try catch finally中return语句与非return语句的执行顺序问题

finally语句一定是会被执行的,不管前边的try块catch块中有无return语句,并且如果finally中存在return语句,这个return语句将会是最后执行的return语句,即函数最后的返回值。try,catch中的return并不是让函数直接返回,而是return语句执行完毕,把返回结果放到函数栈中,转而执行finally块,所以,若是finally中含有return语句,那么函数栈中的返回值将被刷新。看以下几种情况:

1:try有return,finally无

public class TryFinallyReturnTest {
static int test(){
	int x=1;
	try {
		++x;
		return x;
	} catch (Exception e) {
		// TODO: handle exception
	}finally{
		++x;
		System.out.println("finally++x"+x);
	}
	return 0;
}

public static void main(String[] args) {
	System.out.println(new TryFinallyReturnTest().test());
}
}

输出结果:

finally++x3
2

2:当 try 中抛出异常且 catch 中有 return 语句, finally 中没有 return 语句, java 先执行 catch 中非 return 语句,再执行 finally 语句,最后执行 catch 中 return 语句。

public class Test {
public int testTry(){
       FileInputStream fi=null;

       try{
           fi=new FileInputStream("");

       }catch(FileNotFoundException fnfe){
            System.out.println("this is FileNotFoundException");
            return 1;
       }catch(SecurityException se){
           System.out.println("this is SecurityException");
       }finally{
           System.out.println("this is finally");
       }
       return 0;
}

public static void main(String[] args) {
Test t= new Test();
       System. out .println(t.testTry());
}
}
Output :
this is FileNotFoundException
this is finally
1

3:当 try 中抛出异常且 catch 中有 return 语句, finally 中也有 return 语句, java 先执行 catch 中非 return 语句,再执行 finally 中非 return 语句,最后执行 finally 中 return 语句,函数返回值为 finally 中返回的值。

public class Test {
public int testTry(){
       FileInputStream fi=null;

       try{
           fi=new FileInputStream("");

       }catch(FileNotFoundException fnfe){
            System.out.println("this is FileNotFoundException");
            return 1;
       }catch(SecurityException se){
           System.out.println("this is SecurityException");
       }finally{
           System.out.println("this is finally");
             return 3;
       }
       //return 0;
}

public static void main(String[] args) {
Test t= new Test();
       System. out .println(t.testTry());
}
}
Output :
this is FileNotFoundException
this is finally
3

4:另外,throw语句之后不能接任何语句,unreachable。

部分摘自http://blog.csdn.net/leigt3/archive/2010/01/15/5193091.aspx

  

  

  

时间: 2024-08-02 02:42:26

try catch finally中return语句与非return语句的执行顺序问题的相关文章

java中成员变量、代码块、构造函数执行顺序

1.java虚拟机运行程序,首先需要装载类,安装现装载父类,初始化父类的静态代码块和静态成员变量 再load子类.初始化子类静态代码块和成员变量 2.load完毕父类与子类后,从main函数入口执行程序,先输出,接着new实例化Beetle类,则先实例化父类Insect,实例化父类时,先初始化非静态成员变量和非静态代码块,接着执行父类构造方法 再实例化子类,实例化子类时,先初始化非静态成员变量和非静态代码块,接着执行子类构造方法. package it.xiangnan.test; public

Verilog HDL中阻塞语句和非阻塞语句的区别

Verilog语言中讲的阻塞赋值与非阻塞赋值,但从字面意思来看,阻塞就是执行的时候在某个地方卡住了,等这个操作执行完在继续执行下面的语句,而非阻塞就是不管执行完没有,我不管执行的结果是什么,反正我继续下面的事情.而Verilog中的阻塞赋值与非阻塞赋值正好也是这个意思,通过执行一个例子,就可以简单地明白了:1.阻塞赋值可以理解为语句的顺序执行,因此语句的执行顺序很重要2.非阻塞赋值可以理解为语句的并行执行,所以语句的执行不考虑顺序3.在assign的结构中,必须使用的是阻塞赋值 下面给出实例来说

有return语句情况下,try-catch-finally的执行顺序

重要结论: 1.不管有没有出现异常,finally块中代码都会执行 2.当try和catch中有return时,finally仍然会执行 3.finally是在return后面的表达式运算后执行的(此时并没有返回运算后的值,而是先把要返回的值保存起来,不管finally中的代码怎么样,返回的值都不会改变,仍然是之前保存的值),即:函数返回值是在finally执行前确定的 4.finally中最好不要包含return,否则程序会提前退出,返回值不是try或catch中保存的返回值 举例分析: Ca

java 过滤器Filter中chain.doFilter()之前和之后代码的执行顺序

过滤器拦截到响应url的请求后会先执行doFilter()方法中chain.doFilter()之前的代码,然后执行下一个过滤器或者servelt.紧接着执行chain.doFilter()之后的代码. 一下为两个过滤器的执行顺序: 过滤器一: package com.rskd_yswb.lib.filter; import javax.servlet.*; import java.io.IOException; public class HttpRequestAndResponseFilter

VUE生命周期中的钩子函数及父子组件的执行顺序

先附一张官网上的vue实例的生命周期图,每个Vue实例在被创建的时候都需要经过一系列的初始化过程,例如需要设置数据监听,编译模板,将实例挂载到DOM并在数据变化时更新DOM等.同时在这个过程中也会运行一些叫做生命周期钩子的函数(回调函数),这给了用户在不同阶段添加自己代码的机会. 1.vue的生命周期图 在vue实例的整个生命周期的各个阶段,会提供不同的钩子函数以供我们进行不同的操作.先列出vue官网上对各个钩子函数的详细解析. 生命周期钩子 详细 beforeCreate 在实例初始化之后,数

Tip8:Unity中诸如 Awake() Start() Update()等函数的 执行顺序

Unity脚本中有很多的事件函数,下面是各种函数的执行顺序: 1.reset(); 2.Awake(); 3.OnEnable; 4.OnLevelWasLoaded(); 5.Start(); 6.OnApplicationPause(); 7.FixedUpdate(); 8.Update(); 9.LateUpdate(); 10.Rendering(渲染)类 11.Coroutines(协调程序)类 12.OnDestroy(); 13.OnApplicationQuit(); 14.O

Java_try,catch,finally return之间的执行顺序

以往认为函数只要执行到return语句便会返回结果并终止,然而这时错误的,因为这存在特例. 掌握下面几条原则就可以完全解决“当try.catch.finally遭遇return”的问题. 原则:1.finally语句块中的代码是一定会执行的,而catch块中的代码只有发生异常时才会执行. 2. 函数执行完try块中的return语句后不会终止,还会继续执行catch(仅在抛出异常时执行).finally语句块. 3.函数必须确保有唯一返回值 说明: try中如果包含return语句则catch块

springmvc中自定义拦截器以及拦截器的执行过程

1.拦截器在一次请求中的执行流程 2.拦截器入门案例 2.1springMVC.xml的编写 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m

51.try块和catch块中return语句的执行

import java.util.Scanner; /** * 测试try块和catch块中return语句的执行. */ public class Test5 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("请输入被除数:"); try { int num1 = in.nextInt(); System.out.print("请