When should Throwable be used instead of new Exception?

前端 未结 12 1204
没有蜡笔的小新
没有蜡笔的小新 2020-11-28 08:21

Given: Throwable is Exception\'s superclass.

When I read texts on writing your own \'exceptions\', I see examples of Throwable

相关标签:
12条回答
  • 2020-11-28 09:04

    Only two places you should see the word Throwable in code:

    public static void main(String args[])
    {
         try
         {
             // Do some stuff
         }
         catch(Throwable t)
         {
    
         }
     }
    

    AND

    public class SomeServlet extends HttpServlet
    {
          public void doPost(HttpRequest request, HttpResponse response)
          {
             try
             {
                 // Do some stuff
             }
             catch (Throwable t)
             {
                  // Log
             }
          }
     }
    
    0 讨论(0)
  • 2020-11-28 09:10

    throw new Exception(); is something you should never do in a catch block, but you may have to or want to do throw new SomeException(throwable); (preserving the full stack trace) instead of throw throwable; in order to conform to the API of your method, e.g. when it declares to throw SomeException but you're calling code that might throw an IOException that you don't want to add to you method's throws clause.

    The probably most common case is new RuntimeException(throwable); to avoid having a throws clause altogether. Many people will tell you this is a horrible abuse because you should be using checked exceptions. IMO they are wrong and checked exceptions are a mistake in the Java language design that just results in ugly, unmaintainable code.

    0 讨论(0)
  • 2020-11-28 09:12

    Oracle gives the answer in its official tutorial ("How to Throw Exceptions").

    First it explains that Throwables is a union of Error and Exception classes, which are 2 different things.

    Next, it makes clear that an Error is something extremely rare, that is almost impossible to recover from.

    When a dynamic linking failure or other hard failure in the Java virtual machine occurs, the virtual machine throws an Error.

    And putting 1+1 together, it politely concludes, that (except for simple Exceptions) there is no point in catching other Throwables, since you can't do anything about them.

    Simple programs typically do not catch or throw Errors.

    0 讨论(0)
  • 2020-11-28 09:19

    Throwable is an interface, not a class. Two classes extend Throwable, Exception and Error.

    The rule is: be as specific as you can when catching exceptions - that means for example catching Exception instead of Throwable, and IOException instead of Exception.

    Don't catch Errors - errors are bugs. Fix the code instead.

    If you have to catch absolutely everything, use "catch Throwable", but this is bad form.

    0 讨论(0)
  • 2020-11-28 09:20

    Generally, you would not throw or catch Throwable. In particular, JVM errors (that extend Error() ) are not meant to be caught by user code unless you are doing weird system-level work.

    Treat "Throwable" as a language artifact. The "Exception" class is named that because it is the one that is intended to be used by programmers when they want a code block to exit "exceptionally" - by not exiting normally or returning a value.

    That includes both regular error situations (by "regular" I mean as opposed to JVM errors) and places where you are using exceptions as a control mechanism.

    0 讨论(0)
  • 2020-11-28 09:21

    You should not really catch an exception and throw a new one as general as "new Exception".

    Instead, if you wish to bubble up the exception just do the following:

    try {
        // Do some stuff here
    }
    catch (DivideByZeroException e) {
        System.out.println("Can't divide by Zero!"); 
    } 
    catch (IndexOutOfRangeException e) { 
        // catch the exception 
        System.out.println("No matching element found.");
    }
    catch (Throwable e) {
        throw e; // rethrow the exception/error that occurred
    }
    

    It is not good practise, I believe, to catch an exception and throw a new exception instead of the one that was raised to your code block, unless you raise a useful custom exception that provides enough context to elude to the cause of the original exception.

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