When should Throwable be used instead of new Exception?

Given: Throwable is Exception's superclass.

When I read texts on writing your own 'exceptions', I see examples of Throwable being used in the catch block and other texts show new Exception() being used in the catch block. I have yet to see an explanation of when one should use each.

My question is this, when should Throwable be used and when should new Exception() be used?

Inside the catch or else block using either:

throw throwable;

or

throw new Exception();

Solution 1:

Always throw an Exception (never a Throwable). You generally don't catch Throwable either, but you can. Throwable is the superclass to Exception and Error, so you would catch Throwable if you wanted to not only catch Exceptions but Errors, that's the point in having it. The thing is, Errors are generally things which a normal application wouldn't and shouldn't catch, so just use Exception unless you have a specific reason to use Throwable.

Solution 2:

(from comments) The issue that brought this up is that I need to pass an 'exception' to a piece of code a coworker is building if a collection does not get built.

In that case, you might want to throw a checked exception. You could throw an Exception, an appropriate existing subclass of it (except RuntimeException and its subclasses which are unchecked), or a custom subclass of Exception (e.g. "CollectionBuildException"). See the Java Tutorial on Exceptions to get up to speed with Java exceptions.

Solution 3:

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.

Solution 4:

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
         }
      }
 }

Solution 5:

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.

Throwable = Error + Exception

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.