Uncatchable ChuckNorrisException

前端 未结 17 2152
时光取名叫无心
时光取名叫无心 2020-12-02 03:34

Is it possible to construct a snippet of code in Java that would make a hypothetical java.lang.ChuckNorrisException uncatchable?

Thoughts that came to m

17条回答
  •  一整个雨季
    2020-12-02 03:59

    After having pondered this, I have successfully created an uncatchable exception. I chose to name it JulesWinnfield, however, rather than Chuck, because it is one mushroom-cloud-laying-mother-exception. Furthermore, it might not be exactly what you had in mind, but it certainly can't be caught. Observe:

    public static class JulesWinnfield extends Exception
    {
        JulesWinnfield()
        {
            System.err.println("Say 'What' again! I dare you! I double dare you!");
            System.exit(25-17); // And you shall know I am the LORD
        }
    }
        
        
    public static void main(String[] args)
    {       
        try
        {
            throw new JulesWinnfield();
        } 
        catch(JulesWinnfield jw)
        {
            System.out.println("There's a word for that Jules - a bum");
        }
    }
    

    Et voila! Uncaught exception.

    Output:

    run:

    Say 'What' again! I dare you! I double dare you!

    Java Result: 8

    BUILD SUCCESSFUL (total time: 0 seconds)

    When I have a little more time, I'll see if I can't come up with something else, as well.

    Also, check this out:

    public static class JulesWinnfield extends Exception
    {
        JulesWinnfield() throws JulesWinnfield, VincentVega
        {
            throw new VincentVega();
        }
    }
    
    public static class VincentVega extends Exception
    {
        VincentVega() throws JulesWinnfield, VincentVega
        {
            throw new JulesWinnfield();
        }
    }
    
    
    public static void main(String[] args) throws VincentVega
    {
        
        try
        {
            throw new JulesWinnfield();
        }
        catch(JulesWinnfield jw)
        {
            
        }
        catch(VincentVega vv)
        {
            
        }
    }
    

    Causes a stack overflow - again, exceptions remain uncaught.

提交回复
热议问题