Last active
June 6, 2017 08:14
-
-
Save jabrena/164f9fee32f554895c1f5bb3d2e6ea63 to your computer and use it in GitHub Desktop.
Unchecked exceptions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CatacrokerTest { | |
public static void main(String[] args){ | |
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { | |
public void run() { | |
System.out.println("Ohh my god, the Catacroker is alive"); | |
} | |
})); | |
//Control loop | |
int counter = 0; | |
while(true) { | |
counter++; | |
//Logic bomb | |
if(counter > 10){ | |
throw new RuntimeException("Catacroker"); | |
} | |
if(counter > 20){ | |
break; | |
} | |
} | |
System.exit(0); | |
} | |
} |
In some environments, Catacroker won...
Test here:
https://www.compilejava.net/
http://www.browxy.com/
Exception in thread "main" java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "shutdownHooks")
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:472)
at java.security.AccessController.checkPermission(AccessController.java:884)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
at java.lang.Runtime.addShutdownHook(Runtime.java:209)
at CatacrokerTest.main(CatacrokerTest.java:5)
But here Catacroker was captured:
https://www.jdoodle.com/online-java-compiler
public class CatacrokerTestV2 {
private static class CataCrokerCapturer extends Thread {
public void run() {
System.out.println("Ohh my god, the Catacroker is alive");
}
}
public static void main(String[] args){
Runtime.getRuntime().addShutdownHook(new CataCrokerCapturer());
//Control loop
int counter = 0;
while(true) {
counter++;
executeBusinessLogic(counter);
if(counter > 20){
break;
}
}
System.exit(0);
}
private static void executeBusinessLogic(final int counter){
//Logic bomb
if(counter > 10){
throw new RuntimeException("Catacroker");
}
}
}
public class CatacrokerTestV3 {
private static class TheMotherOfAllCataCrokerCapturers extends Thread {
public void run() {
System.out.println("Ohh my god, the CatacrokerCapturer failed");
}
}
private static class CataCrokerCapturer extends Thread {
public void run() {
System.out.println("Ohh my god, the Catacroker is alive");
throw new RuntimeException("La lie parda capturando el Catacroker");
}
}
public static void main(String[] args){
Runtime.getRuntime().addShutdownHook(new TheMotherOfAllCataCrokerCapturers());
Runtime.getRuntime().addShutdownHook(new CataCrokerCapturer());
//Control loop
int counter = 0;
while(true) {
counter++;
executeBusinessLogic(counter);
if(counter > 20){
break;
}
}
System.exit(0);
}
private static void executeBusinessLogic(final int counter){
//Logic bomb
if(counter > 10){
throw new RuntimeException("Catacroker");
}
}
}
Exception in thread "main" java.lang.RuntimeException: Catacroker
at CatacrokerTestV2.executeBusinessLogic(CatacrokerTestV2.java:39)
at CatacrokerTestV2.main(CatacrokerTestV2.java:26)
Ohh my god, the Catacroker is alive
Exception in thread "Thread-1" java.lang.RuntimeException: La lie parda capturando el Catacroker
at CatacrokerTestV2$CataCrokerCapturer.run(CatacrokerTestV2.java:12)
Ohh my god, the CatacrokerCapturer failed
Note: The first hook will run in any case.
Gentle crocker
class Crocker(var alive: Boolean = true){
override fun toString() = if (alive) "I'm alive" else "I'm dead"
}
fun crock(run : Crocker.()-> Unit) : Crocker {
val crocker = Crocker()
try{
crocker.run()
}catch (ex : Throwable ){
println("${ex.message} kill the crocker")
crocker.alive=false;
}
return crocker
}
fun main(args: Array<String>) {
println(crock {
while (true){
throw RuntimeException("catacrok!")
}
})
}
catacrok! kill the crocker
I'm dead
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Normal catacroker
Functional catacroker
Encapsulated catacroker
DSL style croker