File tree Expand file tree Collapse file tree 5 files changed +181
-0
lines changed
core-java-modules/core-java-lang-2/src
main/java/com/baeldung/finallykeyword
test/java/com/baeldung/finallykeyword Expand file tree Collapse file tree 5 files changed +181
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .baeldung .finallykeyword ;
2+
3+ public class FinallyExample {
4+
5+ public void printCount (String count ) {
6+ try {
7+ System .out .println ("The count is " + Integer .parseInt (count ));
8+ } catch (NumberFormatException e ) {
9+ System .out .println ("No count" );
10+ } finally {
11+ System .out .println ("In finally" );
12+ }
13+ }
14+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .finallykeyword ;
2+
3+ public class FinallyExecutedCases {
4+
5+ public void noExceptionFinally () {
6+ try {
7+ System .out .println ("Inside try" );
8+ } finally {
9+ System .out .println ("Inside finally" );
10+ }
11+ }
12+
13+ public void unhandledException () throws Exception {
14+ try {
15+ System .out .println ("Inside try" );
16+ throw new Exception ();
17+ } finally {
18+ System .out .println ("Inside finally" );
19+ }
20+ }
21+
22+ public void handledException () {
23+ try {
24+ System .out .println ("Inside try" );
25+ throw new Exception ();
26+ } catch (Exception e ) {
27+ System .out .println ("Inside catch" );
28+ } finally {
29+ System .out .println ("Inside finally" );
30+ }
31+ }
32+
33+ public String returnFromTry () {
34+ try {
35+ System .out .println ("Inside try" );
36+ return "from try" ;
37+ } finally {
38+ System .out .println ("Inside finally" );
39+ }
40+ }
41+
42+ public String returnFromCatch () {
43+ try {
44+ System .out .println ("Inside try" );
45+ throw new Exception ();
46+ } catch (Exception e ) {
47+ System .out .println ("Inside catch" );
48+ return "from catch" ;
49+ } finally {
50+ System .out .println ("Inside finally" );
51+ }
52+ }
53+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .finallykeyword ;
2+
3+ public class FinallyNotExecutedCases {
4+
5+ public void callingSystemExit () {
6+ try {
7+ System .out .println ("Inside try" );
8+ System .exit (1 );
9+ } finally {
10+ System .out .println ("Inside finally" );
11+ }
12+ }
13+
14+ public void callingRuntimeHalt () {
15+ try {
16+ System .out .println ("Inside try" );
17+ Runtime .getRuntime ()
18+ .halt (1 );
19+ } finally {
20+ System .out .println ("Inside finally" );
21+ }
22+ }
23+
24+ public void infiniteLoop () {
25+ try {
26+ System .out .println ("Inside try" );
27+ while (true ) {
28+ }
29+ } finally {
30+ System .out .println ("Inside finally" );
31+ }
32+ }
33+
34+ public void daemonThread () throws InterruptedException {
35+ Runnable runnable = () -> {
36+ try {
37+ System .out .println ("Inside try" );
38+ } finally {
39+ try {
40+ Thread .sleep (1000 );
41+ System .out .println ("Inside finally" );
42+ } catch (InterruptedException e ) {
43+ e .printStackTrace ();
44+ }
45+ }
46+ };
47+ Thread regular = new Thread (runnable );
48+ Thread daemon = new Thread (runnable );
49+ daemon .setDaemon (true );
50+ regular .start ();
51+ Thread .sleep (300 );
52+ daemon .start ();
53+ }
54+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .finallykeyword ;
2+
3+ public class PitfallsWhenUsingFinally {
4+
5+ public String disregardsUnCaughtException () {
6+ try {
7+ System .out .println ("Inside try" );
8+ throw new RuntimeException ();
9+ } finally {
10+ System .out .println ("Inside finally" );
11+ return "from finally" ;
12+ }
13+ }
14+
15+ public String ignoringOtherReturns () {
16+ try {
17+ System .out .println ("Inside try" );
18+ return "from try" ;
19+ } finally {
20+ System .out .println ("Inside finally" );
21+ return "from finally" ;
22+ }
23+ }
24+
25+ public String throwsException () {
26+ try {
27+ System .out .println ("Inside try" );
28+ return "from try" ;
29+ } finally {
30+ throw new RuntimeException ();
31+ }
32+ }
33+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .finallykeyword ;
2+
3+ import static org .junit .Assert .*;
4+
5+ import org .junit .Test ;
6+
7+ public class PitfallsWhenUsingFinallyUnitTest {
8+
9+ PitfallsWhenUsingFinally instance = new PitfallsWhenUsingFinally ();
10+
11+ @ Test
12+ public void testIgnoresException () {
13+ String result = instance .disregardsUnCaughtException ();
14+ assertEquals ("from finally" , result );
15+ }
16+
17+ @ Test
18+ public void testIgnoresOtherReturns () {
19+ String result = instance .ignoringOtherReturns ();
20+ assertEquals ("from finally" , result );
21+ }
22+
23+ @ Test (expected = RuntimeException .class )
24+ public void testThrowsException () {
25+ instance .throwsException ();
26+ }
27+ }
You can’t perform that action at this time.
0 commit comments