An exceptional condition is a problem that prevents the continuation of the current method or scope. It’s important to distinguish an exceptional condition from a normal problem, when you have enough information in the current context to somehow cope with the difficulty.
When we throw an exception, two things happen.
- The exception object is created in the same way as any Java object: on the heap, with new.
- The current path of execution (the one you can't continue) is stopped and the reference for the exception object is ejected from the current context. Now the exception-handling mechanism takes over and begins to look for an appropriate place(the exception handler) to continue executing the program.
The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement. Similarly, only this class or one of its subclasses can be the argument type in a catch clause. For the purposes of compile-time checking of exceptions, Throwable and any subclass of Throwable that is not also a subclass of eitherRuntimeException
orError
are regarded as checked exceptions.
Instances of two subclasses, Error
and Exception
, are conventionally used to indicate that exceptional situations have occurred. Typically, these instances are freshly created in the context of the exceptional situation so as to include relevant information (such as stack trace data).
The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch.
There are two basic models in exception-handling theory: termination (Java support this) and resumption.
references:
1. On Java 8 - Bruce Eckel
2. https://docs.oracle.com/javase/8/docs/api/java/lang/Throwable.html