posted Aug 29, 2012, 6:06 PM by Neil Mathew
[
updated Aug 29, 2012, 6:09 PM
]
Not part of the list of Amizone questions, but explains a little about the try-catch-finally statements. CONCLUSION:
1) The Finally block of statements WILL run if the error or jump statement is within the try block.
2) In this program, all try statements in functions lack catch statements. Hence, they will be caught by the main function, leaving the function mid way as seen as in Process A. (The finally statements will run, on leaving the try block)
3) If there is a return statement in the try block, the function will end, but the finally statement will run before leaving the function.
SOURCE CODE:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
| package TryCatchExamples;
import java.lang.Exception.*;
class TryCatchExample
{
static void processA()
{
boolean check=false;
System.out.println(" BEG of Process A. \n");
try
{
check=true;
System.out.println(" BEG of Try of Process A. ");
if(check)
throw new RuntimeException("OH MY! AN ERROR!");
System.out.println(" BEG of Try of Process A. ");
}
finally
{
System.out.println(" >Finally of Process A. ");
}
System.out.println("\n END of Process A. ");
}
static void processB()
{
boolean check=true;
System.out.println(" BEG of Process B. \n");
try
{
System.out.println(" BEG of Try of Process B. ");
if(check)
return;
System.out.println(" BEG of Try of Process B. ");
}
finally
{
System.out.println(" >Finally of Process B. ");
}
System.out.println("\n END of Process B. ");
}
static void processC()
{
System.out.println(" BEG of Process C. \n");
try
{
System.out.println(" BEG of Try of Process C. ");
System.out.println(" END of Try of Process C. ");
}
finally
{
System.out.println(" >Finally of Process C. ");
}
System.out.println("\n END of Process C. ");
}
public static void main(String args[])
{
System.out.println("\n Understanding try-catch-finally-throw: ");
System.out.print(" A try-finally in each Process");
System.out.println(", with a try-catch in main: ");
System.out.println("\n Process A: throws Exception ");
System.out.println(" Process B: Return statement in try-catch. ");
System.out.println(" Process C: No Exception. No Return. ");
try
{
System.out.print("\n\n");
processA();
}
catch(Exception e)
{
System.out.println(" Exception Caught at "+e);
System.out.print("\n\n");
processB();
System.out.print("\n\n");
processC();
}
}
} |
OUTPUT:
C:\SomeRandomFolder> java TryCatchExamples.TryCatchExample
Understanding try-catch-finally-throw: A try-finally in each Process, with a try-catch in main: Process A: throws Exception Process B: Return statement in try-catch. Process C: No Exception. No Return. BEG of Process A. BEG of Try of Process A. >Finally of Process A. Exception Caught at java.lang.RuntimeException: OH MY! AN ERROR! BEG of Process B. BEG of Try of Process B. >Finally of Process B. BEG of Process C. BEG of Try of Process C. END of Try of Process C. >Finally of Process C. END of Process C.
|
|
|