From b68bf063dd91b608e09cd872a598784053bb5370 Mon Sep 17 00:00:00 2001 From: Rollczi Date: Sat, 27 Jan 2024 16:19:57 +0100 Subject: [PATCH] Add finally exit method example --- src/test/java/FinallyExitMethod.java | 38 ++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/test/java/FinallyExitMethod.java diff --git a/src/test/java/FinallyExitMethod.java b/src/test/java/FinallyExitMethod.java new file mode 100644 index 0000000..3943e6b --- /dev/null +++ b/src/test/java/FinallyExitMethod.java @@ -0,0 +1,38 @@ +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +public class FinallyExitMethod +{ + + @Test + public void test() + { + assertTrue(finallyExitMethod() == 2); + } + + private static int finallyExitMethod() + { + try + { + throw new Exception(); + } + catch (final Exception exception) + { + System.out.println("catch"); + return printAndGetNumber(1); + } + finally + { + System.out.println("finally"); + return printAndGetNumber(2); + } + } + + private static int printAndGetNumber(int number) + { + System.out.println("number: " + number); + return number; + } + +}