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
This commit is contained in:
parent
bdc82cf661
commit
888539b06d
@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user