New Want to Write for Computaholics ? if Yes Contact us at "contact@computaholics.in"

Java Program to handle the Exception using try and multiple catch block

Java Program to handle the Exception using try and multiple catch block:-

class exception
{
public static void main(String args[]){
try{
int d=42;
int a =0;
int c=d/a;
}
catch(ArithmeticException e){
System.out.println("Division by zero error");
}
}

}


Other Example:
public class ExceptionHandling
{
public static void main(String args[])
{
String num[]={"123","456","abc","789"};
int sum=0;
int i;
for(i=0;i<=num.length;i++)
{
try{
sum+=Integer.parseInt(num[i]);
}
catch(NumberFormatException e)
{ System.out.println("NUMBER FORMAT ERROR");
}
catch(ArrayIndexOutOfBoundsException e)
{System.out.println("ARRAY ERROR");
}
finally
{ System.out.println("i = "+i);
}
}
System.out.println("sum is"+sum);
}
}
Copyright © C Program | Java | OpenGL Programming | Hadoop at Computaholics