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