Unit_2_Exception
Unit_2_Exception
What is exception?
Exception is an abnormal condition.
In java, exception is an event that disrupts the normal flow of the
program. It is an object which is thrown at runtime.
Exception Hierarchy
Types of Exception
There are mainly two types of exceptions: checked and unchecked where
error is considered as unchecked exception. The sun microsystem says
there are three types of exceptions:
1. Checked Exception
2. Unchecked Exception
3. Error
----------------------------------------------------------------------------------------
Catching Exceptions
There are two ways to handle exceptions
Declare exception using throws keyword
Handle them using try/catch block
Syntax:
try
{
// Protected code
}
catch(ExceptionName e1)
{
// Catch block
}
The code that may generate exceptions is placed in the try block.
When an exception occurs, it is handled by catch block associated
with it.
Every try block should be immediately followed either by a catch
block or finally block.
A catch declares the type of exception.
If the type of exception that occurred is listed in a catch block, the
exception is passed to the catch block as an argument.
Example: try/catch block
public class demo
{
public static void main(String args[])
{
try
{
int a[] = new int[2];
System.out.println(a[3]);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Exception thrown:" + e);
}
}
}
Example:
class Example
{
public static void main(String args[])
{
int num1=10;
int num2=0;
int res=num1/num2;
System.out.println(res);
}
}
(* This program would compile successfully but will give error at runtime)
catch(Exception e)
{
System.out.println(e);
}
System.out.println("Program executed with exception.");
}
}
-----------------------------------------------------------------------------------
Syntax:
try
{
// Protected code
}
catch (ExceptionType1 e1)
{
// Catch block
}
catch (ExceptionType2 e2)
{
// Catch block
}
catch (ExceptionType3 e3)
{
// Catch block
}
Example:
class Example
{
public static void main(String args[])
{
int num1=10;
int num2=0;
try
{
int res=num1/num2;
System.out.println(res);
}
catch(ArithmeticException e)
{
System.out.println(e);
}
catch(NumberFormatException e)
{
System.out.println(e);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
}
}
---------------------------------------------------------------------------------------
catch(IOException | FileNotFoundException e)
{
System.out.println(e);
}
Throws/Throw Keywords
If a method does not handle a checked exception, the method must
declare it using the throws keyword.
The throws keyword appears at the end of a method's signature.
You can throw an exception, either a newly instantiated one or an
exception that you just caught, by using the throw keyword.
throws is used to postpone the handling of a checked exception
throw is used to invoke an exception explicitly.
--------------------------------------------------------------------------------
The Finally Block
The finally block follows a try block or a catch block.
Using a finally block allows you to run any cleanup-type statements that
you want to execute, no matter what happens in the protected code.
Syntax:
try
{
// Protected code
}
catch(ExceptionType1 e1) {}
catch(ExceptionType2 e2) {}
finally
{
// The finally block always executes.
}
Example:
public class demo
{
public static void main(String args[])
{
int a[] = new int[2];
try
{
System.out.println(a[3]);
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println( e);
}
finally
{
a[0] = 6;
System.out.println("First element value: "+a[0]);
System.out.println("Finally statement is executed");
}
}
}
O/P
java.lang.ArrayIndexOutOfBoundsException: 3
First element value: 6
Finally statement is executed