`
BBjava
  • 浏览: 126392 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

TryCatchFinally.java 的return 问题。

阅读更多
package org.iteye.bbjava.exception;

public class TryCatchFinally {
	public String returnValue;

	public String getStringValue() {

		try {
			if (true)
				return "the value from try ";
			throw new Exception("Exception!");

		} catch (Exception e) {
			return "the value from catch ";
		} finally {
			return "the value from finally ";
		}
	}

	public static void main(String[] args) {
		String str = new TryCatchFinally().getStringValue();
		System.out.println(str);
	}
}

output:
引用
the value from finally

说明最终返回值是从finally块的return语句返回的。其实try块里有返回过值,请看下面:
引用
javac TryCatchFinally.java
javap -v TryCatchFinally


在终端输出:

引用
Compiled from "TryCatchFinally.java"
public class TryCatchFinally extends java.lang.Object
  SourceFile: "TryCatchFinally.java"
  minor version: 0
  major version: 50
  Constant pool:
const #1 = Method #11.#27; //  java/lang/Object."<init>":()V
const #2 = String #28; //  the value from try
const #3 = String #29; //  the value from finally
const #4 = class #30; //  java/lang/Exception
const #5 = String #31; //  the value from catch
const #6 = class #32; //  TryCatchFinally
const #7 = Method #6.#27; //  TryCatchFinally."<init>":()V
const #8 = Method #6.#33; //  TryCatchFinally.getStringValue:()Ljava/lang/String;
const #9 = Field #34.#35; //  java/lang/System.out:Ljava/io/PrintStream;
const #10 = Method #36.#37; //  java/io/PrintStream.println:(Ljava/lang/String;)V
const #11 = class #38; //  java/lang/Object
const #12 = Asciz returnValue;
const #13 = Asciz Ljava/lang/String;;
const #14 = Asciz <init>;
const #15 = Asciz ()V;
const #16 = Asciz Code;
const #17 = Asciz LineNumberTable;
const #18 = Asciz getStringValue;
const #19 = Asciz ()Ljava/lang/String;;
const #20 = Asciz StackMapTable;
const #21 = class #30; //  java/lang/Exception
const #22 = class #39; //  java/lang/Throwable
const #23 = Asciz main;
const #24 = Asciz ([Ljava/lang/String;)V;
const #25 = Asciz SourceFile;
const #26 = Asciz TryCatchFinally.java;
const #27 = NameAndType #14:#15;//  "<init>":()V
const #28 = Asciz the value from try ;
const #29 = Asciz the value from finally ;
const #30 = Asciz java/lang/Exception;
const #31 = Asciz the value from catch ;
const #32 = Asciz TryCatchFinally;
const #33 = NameAndType #18:#19;//  getStringValue:()Ljava/lang/String;
const #34 = class #40; //  java/lang/System
const #35 = NameAndType #41:#42;//  out:Ljava/io/PrintStream;
const #36 = class #43; //  java/io/PrintStream
const #37 = NameAndType #44:#45;//  println:(Ljava/lang/String;)V
const #38 = Asciz java/lang/Object;
const #39 = Asciz java/lang/Throwable;
const #40 = Asciz java/lang/System;
const #41 = Asciz out;
const #42 = Asciz Ljava/io/PrintStream;;
const #43 = Asciz java/io/PrintStream;
const #44 = Asciz println;
const #45 = Asciz (Ljava/lang/String;)V;

{
public java.lang.String returnValue;

public TryCatchFinally();
  Code:
   Stack=1, Locals=1, Args_size=1
   0: aload_0
   1: invokespecial #1; //Method java/lang/Object."<init>":()V
   4: return
  LineNumberTable:
   line 1: 0


public java.lang.String getStringValue();
  Code:
   Stack=1, Locals=4, Args_size=1
   0: ldc #2; //String the value from try
   2: astore_1
   3: ldc #3; //String the value from finally
   5: areturn
   6: astore_1
   7: ldc #5; //String the value from catch
   9: astore_2
   10: ldc #3; //String the value from finally
   12: areturn
   13: astore_3
   14: ldc #3; //String the value from finally
   16: areturn
  Exception table:
   from   to  target type
     0     3     6   Class java/lang/Exception

     0     3    13   any
     6    10    13   any
    13    14    13   any
  LineNumberTable:
   line 8: 0
   line 14: 3
   line 11: 6
   line 12: 7
   line 14: 10

  StackMapTable: number_of_entries = 2
   frame_type = 70 /* same_locals_1_stack_item */
     stack = [ class java/lang/Exception ]
   frame_type = 70 /* same_locals_1_stack_item */
     stack = [ class java/lang/Throwable ]


public static void main(java.lang.String[]);
  Code:
   Stack=2, Locals=2, Args_size=1
   0: new #6; //class TryCatchFinally
   3: dup
   4: invokespecial #7; //Method "<init>":()V
   7: invokevirtual #8; //Method getStringValue:()Ljava/lang/String;
   10: astore_1
   11: getstatic #9; //Field java/lang/System.out:Ljava/io/PrintStream;
   14: aload_1
   15: invokevirtual #10; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   18: return
  LineNumberTable:
   line 19: 0
   line 20: 11
   line 21: 18


}


整理中……待续.

附上:try -catch-finally,try-finally 的组合是不一样的,比如:
引用

	static int f() {// 这里报编译错误:must return a resutl of type int!
		int id = 0;
		try {
			return id;
		} catch (Exception e) {

		} finally {

		}
	}

	static int f2() {// 但是这里怎么不报 错误呢?
		int id = 0;
		try {
			return id;
		} finally {
		}
	}

参考资料:1.http://topic.csdn.net/u/20091211/15/a8228a73-f2fe-40c0-92f2-1482224f41dd.html
2.http://download.oracle.com/javase/1.4.2/docs/tooldocs/windows/javap.html
分享到:
评论

相关推荐

    Java基础培训教材

    `TryCatchFinally1.java`、`TryCatchFinally.java`、`ExceptionTest.java`、`ThrowableTest.java`、`ExceptionDemo1.java`、`ExceptionDemo2.java`这些文件很可能包含了对异常处理机制的实例演示。Java通过`try-...

    VB.NET的TryCatchFinally使用示例

    VB.NET简单全面的TryCatchFinally使用示例 原文地址:http://www.codeproject.com/KB/dotnet/UsingTryCatchFinally.aspx?azid=86

    trycatch.zip

    在许多语言中,如Java、C#和JavaScript,`try...catch` 结构用于捕获并处理可能出现的异常。然而,原生的Golang并没有直接提供`try...catch`这样的异常处理机制,但我们可以借鉴其他语言的思想,通过自定义的方式来...

    C#例子代码 A0050_TryCatchFinally

    C#例子代码 A0050_TryCatchFinallyC#例子代码 A0050_TryCatchFinallyC#例子代码 A0050_TryCatchFinallyC#例子代码 A0050_TryCatchFinallyC#例子代码 A0050_TryCatchFinallyC#例子代码 A0050_TryCatchFinallyC#例子...

    JSP taglib 精讲

    TagLib,即标记库,是JSP(Java Server Pages)中的一个重要概念。它允许开发者创建自定义的HTML或XML标签,这些标签可以封装复杂的Java代码逻辑,并在JSP页面中像普通HTML标签一样被使用。TagLib的核心思想在于将...

    自定义标签说明例题下载

    当标签执行过程中发生异常时,可以通过` javax.servlet.jsp.tagext.TryCatchFinally`接口的`doFinally()`方法确保必要的清理操作,避免影响其他标签的执行。 总结来说,自定义标签是Java Web开发中提升效率的关键...

    safejson:以异步兼容方式处理 JSON.parsestringify 的库,无需您插入 trycatchfinally

    安全的json 无需 try catch 即可解析和字符串化 JSON 的简洁库。 只需使用提供参数和回调的标准 Node.js 模式,该回调将错误作为第一个参数并将结果作为第二个参数。 这个库非常适合使用 async 链接操作,如下所示...

    TryCatchFinally:探索“尝试”,“捕获”和“最终”以进行错误处理

    1. **避免过于宽泛的catch块**:尽可能具体地捕获异常,以便了解问题所在并采取适当的措施。 2. **记录异常信息**:在catch块中记录异常信息,有助于调试。 3. **使用finally释放资源**:确保finally块用于释放非...

    jsp中的代码重用技术

    - JSP 1.2引入了`IterationTag`和`TryCatchFinally`等新接口,为迭代处理和异常管理提供了更好的支持。 - `BodyTagSupport`接口也得到了增强,更好地支持了主体内容的处理。 #### 六、总结与展望 通过对JSP不同...

    C#多线程教程,经典清析教程

    线程还可以设置优先级,但应谨慎使用,因为过度依赖优先级可能导致调度问题。`Thread.Priority`属性可以用来设置线程优先级。 6. **线程同步事件**:`AutoResetEvent`和`ManualResetEvent`是两种常用的线程同步事件...

    ASP.NET 2.0+SQL Server 2005全程指南-源代码

    2.6.3 用trycatchfinally处理所有的异常 2.7 面向对象程序设计 2.7.1 成员 2.7.2 类的可访问性 2.7.3 构造函数和析构函数 2.7.4 封装 2.7.5 继承 2.7.6 多态 2.8 C#样语言高级特性 2.8.1 接口 2.8.2 ...

Global site tag (gtag.js) - Google Analytics