From 888539b06d17ee19b1563959083bdab389d30bf0 Mon Sep 17 00:00:00 2001 From: rahulgul8 Date: Wed, 1 Jan 2020 16:41:12 +0530 Subject: [PATCH] Changes for "Guide to finally in Java" (BAEL-3526) (#8237) * Adding files for finally keyword * Adding returnFromCatch example. * Adding empty braces instead of dangling ; * Return from try, removing the catch block. * moving to to core-java module * moving to core-java-lang-2 --- .../finallykeyword/FinallyExample.java | 14 +++++ .../finallykeyword/FinallyExecutedCases.java | 53 ++++++++++++++++++ .../FinallyNotExecutedCases.java | 54 +++++++++++++++++++ .../PitfallsWhenUsingFinally.java | 33 ++++++++++++ .../PitfallsWhenUsingFinallyUnitTest.java | 27 ++++++++++ 5 files changed, 181 insertions(+) create mode 100644 core-java-modules/core-java-lang-2/src/main/java/com/baeldung/finallykeyword/FinallyExample.java create mode 100644 core-java-modules/core-java-lang-2/src/main/java/com/baeldung/finallykeyword/FinallyExecutedCases.java create mode 100644 core-java-modules/core-java-lang-2/src/main/java/com/baeldung/finallykeyword/FinallyNotExecutedCases.java create mode 100644 core-java-modules/core-java-lang-2/src/main/java/com/baeldung/finallykeyword/PitfallsWhenUsingFinally.java create mode 100644 core-java-modules/core-java-lang-2/src/test/java/com/baeldung/finallykeyword/PitfallsWhenUsingFinallyUnitTest.java diff --git a/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/finallykeyword/FinallyExample.java b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/finallykeyword/FinallyExample.java new file mode 100644 index 0000000000..bb0bad8cf4 --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/finallykeyword/FinallyExample.java @@ -0,0 +1,14 @@ +package com.baeldung.finallykeyword; + +public class FinallyExample { + + public void printCount(String count) { + try { + System.out.println("The count is " + Integer.parseInt(count)); + } catch (NumberFormatException e) { + System.out.println("No count"); + } finally { + System.out.println("In finally"); + } + } +} diff --git a/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/finallykeyword/FinallyExecutedCases.java b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/finallykeyword/FinallyExecutedCases.java new file mode 100644 index 0000000000..68a3763da0 --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/finallykeyword/FinallyExecutedCases.java @@ -0,0 +1,53 @@ +package com.baeldung.finallykeyword; + +public class FinallyExecutedCases { + + public void noExceptionFinally() { + try { + System.out.println("Inside try"); + } finally { + System.out.println("Inside finally"); + } + } + + public void unhandledException() throws Exception { + try { + System.out.println("Inside try"); + throw new Exception(); + } finally { + System.out.println("Inside finally"); + } + } + + public void handledException() { + try { + System.out.println("Inside try"); + throw new Exception(); + } catch (Exception e) { + System.out.println("Inside catch"); + } finally { + System.out.println("Inside finally"); + } + } + + public String returnFromTry() { + try { + System.out.println("Inside try"); + return "from try"; + } finally { + System.out.println("Inside finally"); + } + } + + public String returnFromCatch() { + try { + System.out.println("Inside try"); + throw new Exception(); + } catch (Exception e) { + System.out.println("Inside catch"); + return "from catch"; + } finally { + System.out.println("Inside finally"); + } + } +} diff --git a/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/finallykeyword/FinallyNotExecutedCases.java b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/finallykeyword/FinallyNotExecutedCases.java new file mode 100644 index 0000000000..92c0ea729d --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/finallykeyword/FinallyNotExecutedCases.java @@ -0,0 +1,54 @@ +package com.baeldung.finallykeyword; + +public class FinallyNotExecutedCases { + + public void callingSystemExit() { + try { + System.out.println("Inside try"); + System.exit(1); + } finally { + System.out.println("Inside finally"); + } + } + + public void callingRuntimeHalt() { + try { + System.out.println("Inside try"); + Runtime.getRuntime() + .halt(1); + } finally { + System.out.println("Inside finally"); + } + } + + public void infiniteLoop() { + try { + System.out.println("Inside try"); + while (true) { + } + } finally { + System.out.println("Inside finally"); + } + } + + public void daemonThread() throws InterruptedException { + Runnable runnable = () -> { + try { + System.out.println("Inside try"); + } finally { + try { + Thread.sleep(1000); + System.out.println("Inside finally"); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + }; + Thread regular = new Thread(runnable); + Thread daemon = new Thread(runnable); + daemon.setDaemon(true); + regular.start(); + Thread.sleep(300); + daemon.start(); + } +} diff --git a/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/finallykeyword/PitfallsWhenUsingFinally.java b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/finallykeyword/PitfallsWhenUsingFinally.java new file mode 100644 index 0000000000..f1a56441f2 --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/finallykeyword/PitfallsWhenUsingFinally.java @@ -0,0 +1,33 @@ +package com.baeldung.finallykeyword; + +public class PitfallsWhenUsingFinally { + + public String disregardsUnCaughtException() { + try { + System.out.println("Inside try"); + throw new RuntimeException(); + } finally { + System.out.println("Inside finally"); + return "from finally"; + } + } + + public String ignoringOtherReturns() { + try { + System.out.println("Inside try"); + return "from try"; + } finally { + System.out.println("Inside finally"); + return "from finally"; + } + } + + public String throwsException() { + try { + System.out.println("Inside try"); + return "from try"; + } finally { + throw new RuntimeException(); + } + } +} diff --git a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/finallykeyword/PitfallsWhenUsingFinallyUnitTest.java b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/finallykeyword/PitfallsWhenUsingFinallyUnitTest.java new file mode 100644 index 0000000000..7e7a7241ec --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/finallykeyword/PitfallsWhenUsingFinallyUnitTest.java @@ -0,0 +1,27 @@ +package com.baeldung.finallykeyword; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class PitfallsWhenUsingFinallyUnitTest { + + PitfallsWhenUsingFinally instance = new PitfallsWhenUsingFinally(); + + @Test + public void testIgnoresException() { + String result = instance.disregardsUnCaughtException(); + assertEquals("from finally", result); + } + + @Test + public void testIgnoresOtherReturns() { + String result = instance.ignoringOtherReturns(); + assertEquals("from finally", result); + } + + @Test(expected = RuntimeException.class) + public void testThrowsException() { + instance.throwsException(); + } +}