Exception_Handling
Exception_Handling
String s=null;
System.out.println(s.length());//NullPointerException
class Ex
{
public static void main(String args[])
{
int a[]=new int[5];
int no=0;
Scanner sc=new Scanner(System.in);
for(int i=0;i<5;i++)
{
System.out.println("Enter the number:");
a[i]=sc.nextInt();
}
for(int i=0;i<5;i++)
{
System.out.println("The number is:"+a[i]);
}
System.out.println("Enter the number to find
index:");
no=sc.nextInt();
System.out.println("The number is:"+a[no]);
}
}
Java Multi-catch block
try{
int a[]=new int[5];
► A try block can be followed a[5]=30/0;
}
by one or more catch
catch(ArithmeticException e)
blocks. Each catch block {
must contain a different System.out.println("Arithmetic Exception
exception handler. So, if you occurs");
have to perform different }
tasks at the occurrence of catch(ArrayIndexOutOfBoundsException e)
different exceptions, use {
java multi-catch block.
System.out.println("ArrayIndexOutOfBounds
Exception occurs");
}
catch(Exception e)
{
System.out.println("Parent Exception
occurs");
}
► Java finally block is a block that is used to
Java finally execute important code such as closing
connection, stream etc.
block ► Java finally block is always executed whether
exception is handled or not.
► Java finally block follows try or catch block.
Example:
try{
int data=25/5;
System.out.println(data);
}
catch(NullPointerException e){System.out.println(e);}
finally{System.out.println("finally block is always executed");}
System.out.println("rest of the code...");
}
Code import java.util.*;
class dddd
without {
public static void main(String[] args) {
int n1;
Exception int ans;
}
catch(ArithmeticException ex)
{
System.out.println("Number can not be divided by zero.");
}
System.out.println("This is a Program of demonstartion of
exception handling.");
}
}
Java Nested
import java.util.*;
try block
class dddd try
{ {
public static void main(String[] args) { int a[]=new int[5];
int n1; System.out.println("Array is:"+a[5]);
int ans; }
catch(ArrayIndexOutOfBoundsException ex)
Scanner sc=new Scanner(System.in); {
System.out.println("Please Enter Number:"); System.out.println("Array Exception arrived.");
n1=sc.nextInt(); }
try
{ }
try catch(Exception e)
{ {
ans=n1/0; System.out.println("Exception occured");
} }
catch(ArithmeticException ex) System.out.println("This is a Program of
{ demonstartion of exception handling.");
System.out.println("Number can not be }
divided by zero."); }
}
Java throw keyword
if(age<18)
throw new ArithmeticException("not valid");
else
System.out.println("welcome to vote");
import java.io.*;
Java throws class M{
keyword void method()throws IOException{
throw new IOException("device error");
}
► The Java throws keyword is }
used to declare an public class Testthrows2{
exception. It gives an public static void main(String args[]){
information to the try{
programmer that there may M m=new M();
occur an exception so it is
m.method();
better for the programmer to
provide the exception }catch(Exception e){System.out.println("exception
handled");}
handling code so that
normal flow can be
maintained. System.out.println("normal flow...");
}
}
In case you declare the exception, if exception does not occur, the code will be
executed fine.
B)In case you declare the exception if exception occures, an exception will be
thrown at runtime because throws does not handle the exception.
import java.io.*;
class M{
void method()throws IOException{
System.out.println("device operation performed");
}
}
class Testthrows3{
public static void main(String args[])throws
IOException{//declare exception
M m=new M();
m.method();
System.out.println("normal flow...");
}
}
Exception Handling:
class TestCustomException1{