Exceptions in Java
An exception in
Java is an event, which occurs during the execution of a program that
disrupts the normal flow of the program.
When an error
occurs within a method, the method creates an object and hands it off to the
runtime system. The object, contains information about the error, including exception
type and the state of the program when the error occurred and is called an exception object. Creating an exception
object and handing it to the runtime system is called throwing
an exception.
The runtime
system searches the call stack for a method that contains a block of code that
can handle the exception. This block of code is called an exception
handler.
An exception can occur for many different reasons. Following are some
scenarios where an exception occurs.
- A user has entered an invalid data.
- A file that needs to be opened cannot be found.
- A network connection has been lost in the middle of communications or the JVM has run out of memory.
The Three Kinds of Exceptions
- Checked exceptions − Checked exception is an exception that occurs at the compile time, and are also called as compile time exceptions. As these exceptions occur at the time of compilation of code, the programmer should take care to handle these exceptions.
- Unchecked exceptions − Unchecked exception is an exception that occurs at the time of execution. As they occur at time of execution they are also called as Runtime Exceptions. These type of exception include programming bugs, such as logic errors or improper use of an API.
- Errors − Errors are not exceptions, but problems that arise beyond the control of the user or the programmer. Errors are typically ignored in the code because you can rarely do anything about an error. For example, out of memory error.
Catching and Handling Exceptions
The exception
mechanism in Java uses three exception handler components — the try, catch, and finally blocks.
The try Block
The try block is
used to enclose the code which might throw an exception.
try {
code
}
If an exception
occurs within the try block, that exception is handled by an
exception handler associated with it. To associate an exception handler with
a try block, you must put a catch block after it.
The Catch Block
We associate
exception handlers with a try block by providing one or more catch blocks
directly after the try block.
try {
//some code that throws Exception
} catch (ExceptionType name) {
// code that is invoked once the
exception is thrown
} catch (ExceptionType name) {
// code that is invoked once the
exception is thrown
}
Each catch block
is an exception handler that handles the type of exception indicated by its
argument.
The catch block
contains code that is executed if and when the exception handler is invoked.
The finally Block
The finally block
always executes when the try block exits.
This ensures that
the finally block is executed even if an unexpected exception occurs.
finally is useful
for more than just exception handling — it allows the programmer to write the clean-up
code , even if there are no exceptions.
finally{
// code for clean up
}
Advantages of Exceptions
- Separating Error-Handling Code from "Regular" Code.
- Propagating Errors Up the Call Stack.
- Grouping and Differentiating Error Types
Exceptions in Java |
No comments:
Post a Comment