Skip to content

Instantly share code, notes, and snippets.

@jabrena
Last active June 6, 2017 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jabrena/164f9fee32f554895c1f5bb3d2e6ea63 to your computer and use it in GitHub Desktop.
Save jabrena/164f9fee32f554895c1f5bb3d2e6ea63 to your computer and use it in GitHub Desktop.
Unchecked exceptions
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);
}
}
@jabrena
Copy link
Author

jabrena commented Jun 4, 2017

Exception in thread "main" java.lang.RuntimeException: Catacroker
	at examples.MetaTest.main(MetaTest.java:16)
Ohh my god, the Catacroker is alive

@juan-medina
Copy link

juan-medina commented Jun 4, 2017

Normal catacroker

fun main(args: Array<String>) {

    Runtime.getRuntime().addShutdownHook(Thread {
        println("ohh my god, the Catacroker is alive")
    })

    var counter = 0
    while (true) {
        counter++

        //Logic bomb
        if (counter > 10) throw RuntimeException("Catacroker")

        if (counter > 20) break
    }

    println("Hello, world!")
}
Exception in thread "main" java.lang.RuntimeException: Catacroker
	at CatacrokerTestKt.main(CatacrokerTest.kt:13)
ohh my god, the Catacroker is alive

Functional catacroker

fun onApplicationShutdown(hook: () -> Unit) = Runtime.getRuntime().addShutdownHook(Thread(hook))

fun alive(name: String) = { println("ohh my god, the $name is alive") }

fun main(args: Array<String>) {

    onApplicationShutdown(alive("Catacroker"))
    onApplicationShutdown(alive("Super Catacroker"))

    var counter = 0
    while (true) {
        counter++

        //Logic bomb
        if (counter > 10) throw RuntimeException("Catacroker")

        if (counter > 20) break
    }

    println("Hello, world!")
}
ohh my god, the Catacroker is alive
Exception in thread "main" java.lang.RuntimeException: Catacroker
ohh my god, the Super Catacroker is alive
	at CatacrokerTestKt.main(CatacrokerTest.kt:18)

Encapsulated catacroker

class ShutdownHook(val name: String) {
    init {
        Runtime.getRuntime().addShutdownHook(Thread(this::onApplicationShutdown))
    }
    fun onApplicationShutdown() = println("ohh my god, the $name is alive")
}

fun Array<String>.createHooks()=this.map(::ShutdownHook)

fun main(args: Array<String>) {

    arrayOf("Catacroker", "Super Catacroker").createHooks()

    var counter = 0
    while (true) {
        counter++

        //Logic bomb
        if (counter > 10) {
            throw RuntimeException("Catacroker")
        }

        if (counter > 20) {
            break
        }
    }

    println("Hello, world!")
}
Exception in thread "main" java.lang.RuntimeException: Catacroker
	at CatacrokerTestKt.main(CatacrokerTest.kt:20)
ohh my god, the Super Catacroker is alive
ohh my god, the Catacroker is alive

DSL style croker

class ShutdownHook(val name: String) {
    init {
        Runtime.getRuntime().addShutdownHook(Thread(this::onApplicationShutdown))
    }

    fun onApplicationShutdown() = println("ohh my god, the $name is alive")
}

class HookBuilder

fun hooks(init: HookBuilder.() -> Unit): HookBuilder {
    val hookBuilder = HookBuilder()
    hookBuilder.init()
    return hookBuilder
}

class ShutdownHookElement {
    operator fun String.unaryPlus() = ShutdownHook(this)
}

fun shutdown(init: ShutdownHookElement.() -> Unit): ShutdownHookElement {
    val hook = ShutdownHookElement()
    hook.init()
    return hook
}

fun main(args: Array<String>) {

    hooks {
        shutdown { + "Catacroker" }
        shutdown { + "Super Catacroker" }
    }

    var counter = 0
    while (true) {
        counter++

        //Logic bomb
        if (counter > 10) {
            throw RuntimeException("Catacroker")
        }

        if (counter > 20) {
            break
        }
    }

    println("Hello, world!")
}
Exception in thread "main" java.lang.RuntimeException: Catacroker
	at CatacrokerTestKt.main(CatacrokerTest.kt:39)
ohh my god, the Super Catacroker is alive
ohh my god, the Catacroker is alive

@jabrena
Copy link
Author

jabrena commented Jun 5, 2017

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

@jabrena
Copy link
Author

jabrena commented Jun 5, 2017

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");
        }
    }
}

@jabrena
Copy link
Author

jabrena commented Jun 5, 2017

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.

@juan-medina
Copy link

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