Exception handling
Exception handling is the process of responding to unwanted or unexpected
events when a computer program runs. Exception handling deals with these
events to avoid the program or system crashing, and without this process,
exceptions would disrupt the normal operation of a program.
• Exception handling in java is a mechanism to handle unwanted
interruptions like exceptions and continue with the normal flow of the
program.
• Java uses try-catch blocks and other keywords like finally, throw, and
throws to handle exceptions.
• JVM(Java Virtual Machine) by default handles exceptions, when an
exception is raised it will halt the execution of the program and throw
the occurred exception.
Example of Exception Handling in Java:
class ExceptionExample {
public static void main(String args[]) {
try {
// Code that can raise exception
int div = 509 / 0;
} catch (ArithmeticException e) {
System.out.println(e);
System.out.println("End of code");
java.lang.ArithmeticException: / by zero
Output:
End of code
Java Programming
How Exception handling works:
o Java Exception Keywords and Examples
1. try block
• try block is used to execute doubtful statements which can throw
exceptions.
• try block can have multiple statements.
• Try block cannot be executed on itself, there has to be at least one
catch block or finally block with a try block.
• When any exception occurs in a try block, the appropriate exception
object will be redirected to the catch block, this catch block will handle
the exception according to statements in it and continue the further
execution.
• The control of execution goes from the try block to the catch block once
an exception occurs.
Java Programming
Syntax:
2.Catch block
• catch block is used to give a solution or alternative for an exception.
• catch block is used to handle the exception by declaring the type of
exception within the parameter.
• The declared exception must be the parent class exception or the
generated exception type in the exception class hierarchy or a user-
defined exception.
• You can use multiple catch blocks with a single try block.
Hierarchy of Java Exception Classes
• As java is an object-oriented language every class extends the Object
class.
• All exceptions and errors are subclasses of class Throwable.
• Throwable is the base/superclass of exception handling hierarchy in
java.
• This class is further extended in different classes like exception, error,
and so on.
Java Programming
❖ Types of Exceptions Handling in Java
1. Checked Exceptions
• Checked exceptions are those exceptions that are checked at compile
time by the compiler.
• The program will not compile if they are not handled.
• These exceptions are child classes of the Exception class.
• IOException,ClassNotFoundException, InvocationTargetException, and
SQL Exception are a few of the checked exceptions in Java.
2. Unchecked Exceptions
• Unchecked exceptions are those exceptions that are checked at run
time by JVM, as the compiler cannot check unchecked exceptions,
• The programs with unchecked exceptions get compiled successfully but
they give runtime errors if not handled.
• These are child classes of Runtime Exception Class.
Java Programming
• ArithmeticException,NullPointerException,NumberFormatException,
IndexOutOfBoundException are a few of the unchecked exceptions in
Java.
Difference between Checked and Unchecked Exceptions: