Chapter 4
Exception Handling
Exception Handling
Topics covered:
What exceptions are and how they’re
handled
The exception class hierarchy
When to use exception handling:
try blocks to delimit code in which
exceptions might occur
throw exceptions to indicate a problem
catch blocks to specify exception
handlers
finally block to release resources
Exception Handling
What is Exception Handling in Java?
Exception handling is a mechanism to handle the
run time errors so that the normal flow of the
application can be maintained
Exception Handling
Exception
Exception is an indication of a problem that
occurs during a program’s execution
Exception handling enables you to create
applications that can resolve (or handle)
exceptions
Handling an exception allows a program to
continue executing as if no problem had been
encountered
Exception Handling
Exception
An exception can occur by:
User error – e.g.: a user has entered an
invalid data
Programmer error – e.g.: a file that needs
to be opened cannot be found
Physical resources failure – e.g.: a
network connection has been lost in the
middle of communications or the JVM has
run out of memory
Exception Handling
Types of Java Exceptions
According to Oracle, there are three types of exceptions:
1. Checked Exception.
• A checked exception is an exception that is checked
(notified) by the compiler at compilation-time, these are
also called as compile time exceptions. These exceptions
cannot simply be ignored, the programmer should take
care of (handle) these exceptions
2. Unchecked Exception
• An unchecked exception is an exception that occurs at the
time of execution. These are also called as Runtime
Exceptions. These include programming bugs, such as logic
errors or improper use of an API. Runtime exceptions are
ignored at the time of compilation
3. Error
• These are not exceptions at all, but problems that arise
beyond the control of the user or the programmer.
Errors are typically ignored in your code because you can
rarely do anything about an error. For example, if a stack
overflow occurs, an error will arise. They are also ignored at
the time of compilation
Exception Handling
Checked
exceptio
n
Unchecke
d
exception
Exception Handling
Java Exception Keywords
There are 5 keywords which are used in handling exceptions in
Java
try The "try" keyword is used to specify a block where we should place
exception code. The try block must be followed by either catch or
finally. It means, we can't use try block alone.
catch The "catch" block is used to handle the exception. It must be
preceded by try block which means we can't use catch block alone. It
can be followed by finally block later.
finally The "finally" block is used to execute the important code of the
program. It is executed whether an exception is handled or not.
throw The "throw" keyword is used to throw an exception.
throws The "throws" keyword is used to declare exceptions. It doesn't throw
an exception. It specifies that there may occur an exception in the
method. It is always used with method signature.
Exception Handling
Syntax:
try{
//code that may throw an exception
try-catch }
block catch(Exception_class_Name ref){
//code to respond to exception
};
try{
//code that may throw an exception
try-finally }
block finally(Exception_class_Name ref){
//code to respond to exception
};
Example - Exception Handling
Example – finally block
Exception Handling
Java throw exception
The Java throw keyword is used to explicitly throw an
exception.
Throw keyword can be use on either checked or
unchecked exception.
The throw keyword is mainly used to throw custom
exception.
throw Exception_class_object;
Example
throw new IOException(“sorry device error”);
Example – using throw keyword
Example - Exception Handling in GUI
Example - Exception Handling in GUI