Skip to content

Commit 4e3d2e6

Browse files
committed
Probelm 02 - Ch 13
1 parent 3121aca commit 4e3d2e6

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ Java Solutions for Cracking the Coding Interview - 6th Edition
129129

130130
**Ch 13 - Java**
131131
- [X] [01 - Private Constructor](../master/src/com/deepak/ctci/Ch13_Java/Problem_01.java)
132-
- [ ] 02 - Return from Finally
132+
- [X] [02 - Return from Finally](../master/src/com/deepak/ctci/Ch13_Java/Problem_02.java)
133133
- [ ] 03 - Final etc
134134
- [ ] 04 - Generics v/s Templates
135135
- [ ] 05 - TreeMap, HashMap, LinkedHashMap

src/com/deepak/ctci/Ch13_Java/Problem_02.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,27 @@
1616
*/
1717
public class Problem_02 {
1818

19+
/* Yes, finally block will always be executed even if try block has,
20+
* 1. return statement
21+
* 2. exception
22+
* 3. break statement
23+
* 4. continue statement
24+
*
25+
* There are only 2 possible ways where a finally block may not get executed. They are,
26+
* 1. If the child thread processing the try block dies.
27+
* 2. if the VM executing the process, gets killed */
28+
29+
public static void main(String[] args) {
30+
int x = 0;
31+
int y = 5;
32+
try {
33+
int result = y/x;
34+
System.out.println("Divison success !!. Result : " + result);
35+
} catch (Exception exception) {
36+
System.out.println("Exception occured. Message : " + exception.getMessage());
37+
} finally {
38+
System.out.println("Finally block executed even when there was a exception in try block !!");
39+
}
40+
}
41+
1942
}

0 commit comments

Comments
 (0)