Exception Handling in Java
The Exception Handling in Java is one of the powerful mechanisms to handle the runtime
errors so that normal flow of the application can be maintained.
What is Exception in Java
Exception is an abnormal condition.
In Java, an exception is an event that disrupts the normal flow of the program. It is an object
which is thrown at runtime.
What is Exception Handling
Exception Handling is a mechanism to handle runtime errors such as
ClassNotFoundException, IOException, SQLException, RemoteException, etc.
Advantage of Exception handling:
The core advantage of exception handling is to maintain the normal flow of the
application. An exception normally disrupts the normal flow of the application that is why
we use exception handling.
Example:
Five Keyword:
TRY
CATCH
THROW
THROWS
FINALLY
Syntax:
Try
{
…….
…….
…….
}
Catch (exception e)
{
……
……
……
}
Types of Java Exceptions
1. Checked Exception
2. Unchecked Exception
3. Error
Runtime Exceptions are:
Arthmetic Exception
NumberFormatException
ArrayIndexOutOfBoundsException
NullPointerException
1. Arthmetic Exception
int a=50/0;//ArithmeticException
2. NullPointerException
String s=null;
[Link]([Link]());//NullPointerException
3. NumberFormatException
String str="Hello";
int i=[Link](str);//NumberFormatException
4. ArrayIndexOutOfBoundsException
int a[]=new int[5];
a[10]=50; //ArrayIndexOutOfBoundsException
Example 1:
public class JavaExceptionExample{
public static void main(String args[]){
try{
//code that may raise exception
int data=100/0;
}catch(ArithmeticException e){[Link](e);}
//rest code of the program
[Link]("rest of the code...");
}
}
Example 2: ArithmeticException
class Ex
{
void display()
{{
int a=5/0;
}
catch(ArithmeticException e)
{
[Link](e);
}
}}
class Except
{
public static void main(String arg[])
{
Ex obj=new Ex();
[Link]();
}
}
Example 3: ArrayIndexOutOfBoundsException
class Ex
{
void display()
{
try
{
int a[]=new int[5];
a[6]=5;
}
catch(ArrayIndexOutOfBoundsException e)
{
[Link](e);
}
[Link]("Exception handling...");
}}
class Except1
{
public static void main(String arg[])
{
Ex obj=new Ex();
[Link]();
}
}
Example 4: NumberFormatException
class Ex
{
void display()
{
try
{
String str="hello";
int num=[Link](str);
}
catch(NumberFormatException e)
{
[Link](e);
}
[Link]("Exception handling...");
}}
class Except2
{
public static void main(String arg[])
{
Ex obj=new Ex();
[Link]();
}
}
Example 4:NullPointer Exception
class Ex
{
void display()
{
try
{
String str=Null;
[Link]([Link]());
}
catch(NumberFormatException e)
{
[Link](e);
}
[Link]("Exception handling...");
}}
class Except2
{
public static void main(String arg[])
{
Ex obj=new Ex();
[Link]();
}
}
Java throw block:
public class TestThrow1{
static void validate(int age){
if(age<18)
throw new ArithmeticException("not valid");
else
[Link]("welcome to vote");
}
public static void main(String args[]){
validate(13);
[Link]("rest of the code...");
}
}
Java throws block:
The Java throws keyword is used to declare an exception. It gives an information to the
programmer that there may occur an exception so it is better for the programmer to
provide the exception handling code so that normal flow can be maintained.
Public class javaExceptionExample
{
Void numcheck(int num) throws IOEXception ,classNotException
{
if(num==1)
{
throw new (“IOEXception”);
else
{
throw new (“Class Not found”);
}}
Public static void main(String arg[])
{
javaExceptionExample obj=new javaExceptionExample();
try
{
[Link](1);
}
Catch(Exception e)
{
[Link](e);
}
Java finally block
Java finally block is always executed whether exception is handled or not.
Java finally block follows try or catch block.
Example 1: where exception occurs and not handled.
class TestFinallyBlock{
public static void main(String args[]){
try{
int data=25/5;
[Link](data);
}
catch(NullPointerException e){[Link](e);}
finally{[Link]("finally block is always executed");}
[Link]("rest of the code...");
}
}
Output:
5
finally block is always executed
rest of the code...