Uncatchable ChuckNorrisException

前端 未结 17 2001
时光取名叫无心
时光取名叫无心 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:50

    No. All exceptions in Java must subclass java.lang.Throwable, and although it may not be good practice, you can catch every type of exception like so:

    try {
        //Stuff
    } catch ( Throwable T ){
        //Doesn't matter what it was, I caught it.
    }
    

    See the java.lang.Throwable documentation for more information.

    If you're trying to avoid checked exceptions (ones that must be explicitly handled) then you will want to subclass Error, or RuntimeException.

    0 讨论(0)
  • 2020-12-02 03:51

    The only ChuckNorrisExceptions in Java should be OutOfMemoryError and StackOverflowError.

    You can actually "catch" them in the means that a catch(OutOfMemoryError ex) will execute in case the exception is thrown, but that block will automatically rethrow the exception to the caller.

    I don't think that public class ChuckNorrisError extends Error does the trick but you could give it a try. I found no documentation about extending Error

    0 讨论(0)
  • 2020-12-02 03:53

    Call System.exit(1) in the finalize, and just throw a copy of the exception from all the other methods, so that the program will exit.

    0 讨论(0)
  • 2020-12-02 03:54

    With such an exception it would obviously be mandatory to use a System.exit(Integer.MIN_VALUE); from the constructor because this is what would happen if you threw such an exception ;)

    0 讨论(0)
  • 2020-12-02 03:55

    A variant on the theme is the surprising fact that you can throw undeclared checked exceptions from Java code. Since it is not declared in the methods signature, the compiler won't let you catch the exception itself, though you can catch it as java.lang.Exception.

    Here's a helper class that lets you throw anything, declared or not:

    public class SneakyThrow {
      public static RuntimeException sneak(Throwable t) {
        throw SneakyThrow.<RuntimeException> throwGivenThrowable(t);
      }
    
      private static <T extends Throwable> RuntimeException throwGivenThrowable(Throwable t) throws T {
        throw (T) t;
      }
    }
    

    Now throw SneakyThrow.sneak(new ChuckNorrisException()); does throw a ChuckNorrisException, but the compiler complains in

    try {
      throw SneakyThrow.sneak(new ChuckNorrisException());
    } catch (ChuckNorrisException e) {
    }
    

    about catching an exception that is not thrown if ChuckNorrisException is a checked exception.

    0 讨论(0)
  • 2020-12-02 03:56

    Two fundamental problems with exception handling in Java are that it uses the type of an exception to indicate whether action should be taken based upon it, and that anything which takes action based upon an exception (i.e. "catch"es it) is presumed to resolve the underlying condition. It would be useful to have a means by which an exception object could decide which handlers should execute, and whether the handlers that have executed so far have cleaned things up enough for the present method to satisfy its exit conditions. While this could be used to make "uncatchable" exceptions, two bigger uses would be to (1) make exceptions which will only be considered handled when they're caught by code that actually knows how to deal with them, and (2) allow for sensible handling of exceptions which occur in a finally block (if a FooException during a finally block during the unwinding of a BarException, both exceptions should propagate up the call stack; both should be catchable, but unwinding should continue until both have been caught). Unfortunately, I don't think there would be any way to make existing exception-handling code work that way without breaking things.

    0 讨论(0)
提交回复
热议问题