Exception Handling PPT-1
Exception Handling PPT-1
Dr Amit Yadav
Exception Handling
Exception Handling
1. Checked Exception
2. Unchecked Exception
3. Error
Difference between checked and
unchecked exceptions
Checked Exception: Checked exceptions are checked at compile-time.
Java try block is used to enclose the code that might throw an exception. It
must be used within the method.
➢ NullPointerException
String s=null;
System.out.println(s.length()); //NullPointerException
➢ NumberFormatException
String s="abc";
int i=Integer.parseInt(s); //NumberFormatException
➢ ArrayIndexOutOfBoundsException
try
{
//code that may throwexception
}
catch(Exception_class_Name ref)
{
}
Syntax of try-finally block
try
{
//code that may throwexception
}
finally
{
}
Problem without exception handling
Output:
Exception in thread main java.lang.ArithmeticException:/ byzero
Solution by exception handling
public class Testtrycatch2
{
public static void main(String args[])
{
try{
int data=50/0;
}
catch(ArithmeticException e)
{
System.out.println(e);
}
System.out.println("rest of the code...");
}
}
Output:
Exception in thread main java.lang.ArithmeticException:/ by zero rest of the code...
Java Multi catch block
public classTestMultipleCatchBlock{
public static void main(Stringargs[]){
try
{
int a[]=new int[5];
a[5]=30/0;
}
catch(ArithmeticException e)
{
System.out.println("task1 iscompleted");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("task 2 completed");
}
catch(Exception e)
{
System.out.println("common taskcompleted");
}
System.out.println("rest of the code...");
}}
Output:
task1 completed rest of the code...
Java Nested try block
➢ In Java, using a try block inside another try block is permitted.
System.out.println("other statement");
}
//catch block of outer try block
catch(Exception e)
{
System.out.println("handled the exception (outer catch)");
}
System.out.println("normal flow..");
}
}
Output:
Java finally block
➢ Java finally block is a block used to execute important code such as
closing the connection, etc.
➢ finally block in Java can be used to put "cleanup" code such as closing a
file, closing connection, etc.
try {
Output:
Java throw keyword
➢ Syntax:
throw new exception_class("error message");
➢ Ex:
throw new IOException("sorry device error");
Throwing Unchecked Exception
public class TestThrow1 {
//function to check if person is eligible to vote or not
public static void validate(int age) {
if(age<18) {
//throw Arithmetic exception if not eligible to vote
throw new ArithmeticException("Person is not eligible to vote");
}
else {
System.out.println("Person is eligible to vote!!");
}
}
//main method
public static void main(String args[]){
//calling the function
validate(13);
System.out.println("rest of the code...");
}
}
Output:
Java Exception Propagation
➢ An exception is first thrown from the top of the stack and if it is not
caught, it drops down the call stack to the previous method
class TestExceptionPropagation{
void m(){
int data=50/0;
}
void n(){
m();
}
void p(){
try{
n();
}catch(Exception e){System.out.println("exception handled");}
}
public static void main(String args[]){
TestExceptionPropagation1 obj=new TestExceptionPropagation1();
obj.p();
System.out.println("normal flow...");
}
}
Java throws keyword
➢ The Java throws keyword is used to declare an exception.
➢ Syntax:
Output:
Difference between final, finally and finalize
➢ The final, finally, and finalize are keywords in Java that are used in
exception handling.
➢ The basic difference between final, finally and finalize is that the final is
an access modifier, finally is the block in Exception Handling
and finalize is the method of object class.
Java Custom Exception
➢ In Java, we can create our own exceptions that are derived classes of the
Exception class.