Exceptions
-
Exception Overview:
- An exception in Java is a signal to the Java Virtual Machine (JVM) that a program has violated the semantic constraints of the Java programming language.
- Exceptions provide a means for error handling and are used to transfer control from the point where an error occurred to a point that can handle it.
-
Exception Terminology:
- An exception is said to be “thrown” from where it occurred and “caught” at the point to which control is transferred.
- Programs can explicitly throw exceptions using
throw
statements.
-
Exception Classes:
- All exceptions are represented by instances of the class
Throwable
or one of its subclasses. Exception
andError
are direct subclasses ofThrowable
.Exception
: Superclass of all exceptions from which a program may wish to recover.Error
: Superclass of exceptions from which a program is not expected to recover.
RuntimeException
: Direct subclass ofException
, superclass of all exceptions that may be thrown during expression evaluation but from which recovery may be possible.
- All exceptions are represented by instances of the class
-
Checked vs. Unchecked Exceptions:
- Checked exceptions are exceptions that must be either caught or declared in the method’s
throws
clause. - Unchecked exceptions are not checked at compile time and include
RuntimeException
andError
.
- Checked exceptions are exceptions that must be either caught or declared in the method’s
-
Exception Handling:
- Exception handling is done using
try
,catch
, andfinally
blocks. - The JVM will search for a
catch
block that can handle the thrown exception starting from the nearest enclosingtry
block.
- Exception handling is done using
-
Compile-Time Checking:
- The Java compiler checks for the presence of handlers for checked exceptions that can be thrown by a method or constructor.
- If a method or constructor can throw a checked exception and does not handle it, it must declare it with a
throws
clause.
-
Asynchronous Exceptions:
- Asynchronous exceptions are exceptions that can occur at any point in the execution of a program.
- They can be caused by invoking the deprecated
stop
method ofThread
orThreadGroup
, or by an internal error or resource limitation in the JVM.
-
Run-Time Handling:
- When an exception is thrown, control is transferred to the nearest dynamically enclosing
catch
clause that can handle the exception. - If no
catch
clause is found, the current thread is terminated, and uncaught exception handlers are invoked.
- When an exception is thrown, control is transferred to the nearest dynamically enclosing
-
Finally Block:
- A
finally
block is used to ensure that a block of code is always executed after another block, even if the latter completes abruptly. - If a
try
orcatch
block completes abruptly, thefinally
block is executed during the propagation of the exception.
- A
-
Exception Propagation:
- If an exception is not caught by any
catch
block, it propagates up the call stack until it is caught or the thread is terminated.
- If an exception is not caught by any
See
https://docs.oracle.com/javase/specs/jls/se7/html/jls-11.html