This discussion is archived
5 Replies Latest reply: Feb 22, 2013 8:41 PM by rp0428 RSS

throwable

808202 Newbie
Currently Being Moderated
Hi All,

I'm basically new to Java & wanted know why do we need Exception class when already there is throwble. Exception has 4 constructor only & same is present in throwable as well. what is the need of exception class?

Thanks,
Karthik
  • 1. Re: throwable
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    <tt>Throwable</tt> is extended not only by <tt>Exception</tt> but also by <tt>Error</tt> wich has another use case...

    bye
    TPD
  • 2. Re: throwable
    808202 Newbie
    Currently Being Moderated
    You are correct but we can achieve the same by simply extending throwable class in our custom exception class. Something like:

    MyException extends Throwable{ }

    what is the use in doing MyException extends Exception?
  • 3. Re: throwable
    Kayaman Guru
    Currently Being Moderated
    Exceptions and Errors are two different breeds, with Error being the more serious one.

    In normal work, there is never a need to extend Error.
  • 4. Re: throwable
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    user12252224 wrote:
    You are correct but we can achieve the same by simply extending throwable class in our custom exception class.
    However conceptually it would be incorrect.

    If you have a custom exception class then it has to mean something. And extending Throwable isn't a correct representation of any reasonable nature for a java application (versus a VM.)

    If you want an alternative semantics to Exception then you should use RuntimeException.
  • 5. Re: throwable
    rp0428 Guru
    Currently Being Moderated
    >
    You are correct but we can achieve the same by simply extending throwable class in our custom exception class. Something like:

    MyException extends Throwable{ }

    what is the use in doing MyException extends Exception?
    >
    Perhaps if you read Chapter 11 'Exceptions' in the Java Specification it will make it more clear to you
    http://docs.oracle.com/javase/specs/jls/se7/jls7.pdf
    >
    11.1.1 The Kinds of Exceptions

    An exception is represented by an instance of the class Throwable (a direct subclass of Object) or one of its subclasses.
    Throwable and all its subclasses are, collectively, the exception classes.
    Note that a subclass of Throwable must not be generic (§8.1.2).

    The classes Exception and Error are direct subclasses of Throwable.

    Exception is the superclass of all the exceptions from which ordinary programs
    may wish to recover.

    Error is the superclass of all the exceptions from which ordinary programs are not
    ordinarily expected to recover.

    Error and all its subclasses are, collectively, the error classes.

    The class Error is a separate subclass of Throwable, distinct from Exception in the class
    hierarchy, to allow programs to use the idiom "} catch (Exception e) {" (§11.2.3)
    to catch all exceptions from which recovery may be possible without catching errors from
    which recovery is typically not possible.

    The class RuntimeException is a direct subclass of Exception.
    RuntimeException is the superclass of all the exceptions which may be thrown
    for many reasons during expression evaluation, but from which recovery may still
    be possible.
    >
    Note the next to last paragraph that starts with 'The class Error is a separate subclass of Throwable . . .'.

    That paragraph tells you EXACTLY why there is both an Error class and an Exception class.
    And, because both Error and Exception have a lot in common that commonality is expressed in
    the superclass Throwable.

    As that paragraph says programs will use "} catch (Exception e) {" to catch and handle problems
    that you may be able to recover from.

    If you need to better identify and deal with your own problems you would extend the Exception
    class since your own problems will be exceptions, not 'Errors'.

    Sure, you could write your own classes to extend from Throwable but everything you did would be
    non-standard and would serve no purpose at all other than just 'being different'.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points