Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

EXCEPTIONS

INDEX :
● Exception
● What happens when an exception occurs ?
● Important exceptions
● Checked exception
● Unchecked exception
● Throwable hierarchy
EXCEPTION

EXCEPTION :
The exception is a problem that occurs during the execution of a program (Runtime). When an
exception occurs, the execution of the program stops abruptly (Unexpected stop).
NOTE :
Every exception in java is a class of 'Throwable type'
WHAT HAPPENS IF AN EXCEPTION OCCURS ?

NORMAL EXECUTION

YES NORMAL EXECUTION


ABNORMAL
? PAUSED

A THROWABLE TYPE
NO OBJECT IS CREATED

YES IF
NO
EXCEPTION
IS HANDLED
?
NORMAL
EXECUTION

ABRUPT STOP
STOP
NOTE :
● Every exception is occurred because of a statement.
● A statement will throw an exception during the abnormal situation.
EXAMPLE :
import java.util.Scanner ;
Class Demo
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int a , b ;
a = input.nextInt();
b = input.nextInt();
int c = a / b ; // Statement might cause Exception(ArithmeticException)
System.out.println(“The division of ”+a+” and “+b+” = “+c);
}
} CASE 1 CASE 2

a = 5 , b = 10 ; a=5,b=0;
->Normal situation ->Abnormal situation
->No exception ->Exception occurs
IMPORTANT EXCEPTIONS AND STATEMENTS

STATEMENT EXCEPTION
a/b ArithmeticException
reference.member NullPointerException
(ClassName)reference ClassCastException
array_ref[index] ArrayIndexOutOfBoundsException
string.charAt(index) StringIndexOutOfBoundsException
string.substring(Index) StringIndexOutOfBoundsException
CHECKED EXCEPTION

CHECKED EXCEPTION :
The compiler-aware exception is known as the checked exception. i.e., the
Compiler knows the statement responsible for abnormal situations (Exception).
Therefore the compiler forces the programmer to either handle or declare the
exception. If it is not done we will get an unreported compile-time error.
Example : FileNotFoundException
UNCHECKED EXCEPTION

UNCHECKED EXCEPTION :
The compiler-unaware exception is known as the unchecked exception. i.e., the Compiler
doesn’t know the statements which are responsible for abnormal situations (Exception). Hence, the
compiler will not force the programmer either to handle or declare the exception.
Example : ArithmeticException
Throwable

Exception Error

RuntimeException IOException SQLException AWTException


VirtualMachineError

StackOverflowError
ArithmeticException FileNotFoundException InterruptedException
OutOfMemoryError
NullPointerException InterruptedIOException
AssertionError
ClassCastException EOFException
ExceptionInInitializerError
IndexOutOfBoundsException
IOError

ArrayIndexOutOfBoundsException AWTError

StringIndexOutOfBoundsException
NOTE :
● In Throwable hierarchy Error class and its subclasses, RuntimeException class and its
subclasses are all considered as Unchecked Exceptions.
● All the subclasses of the Exception class except RuntimeException are considered as
Checked Exceptions.
● Throwable and Exception classes are partially checked and partially unchecked.

You might also like