Tutoriallearn.com
Easy Learning
Search Tutoriallearn.com :
class A
{
public static void main(String args[])
{
int a = 0;
int b =20;
int c = b/a;
System.out.println(c);
}
}
class A
{
public static void main(String args[])
{
try {
int a = 0;
int b =20;
int c = b/a;
System.out.println(c);
}
catch(ArithmeticException e)
{
System.out.println("divided by zero" + e) ;
}
}
}
import java.util.Scanner;
public class demoexcep
{
public static void main(String args[])
{
try {
Scanner sc = new Scanner(System.in);
int d, a;
int c[] = new int[1];
d = sc.nextInt();
a= 10/d; // Arithmetic Exception if input value of d is 0
c[0]= 1 ;
c[42]= 100; // Array index out of Bounds Exception raised
}
catch(ArithmeticException e)
{
System.out.println("Divide by Zero Error");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array index out of Bounds Exception "+e);
}
System.out.println("After catch");
}
}
Two catch statements with one try statement are used in this program. A try statement may have more than one catch statement.
public class demoexcep {
public static void main(String args[]) {
try {
int m = args.length;
System.out.println("m = " + m);
try { // nested try block
// In case one command-line arg, then a divide-by-zero exception
if(m==1) {
m = m/(m-m); // division by zero
}
// If two command-line args are used, then out-of-index bounds exception.
if(m==2) {
int c[] = { 1 }; // Array is defined with one element
c[2] = 25; // storing data beyond array indexing, exception will raised
}
} catch(ArrayIndexOutOfBoundsException e) { // catch of inner try block
System.out.println("Array index out-of-bounds: " + e);
}
} catch(ArithmeticException e) { // catch of outer try block
System.out.println("Divide by 0: " + e);
}
} //end of main
}// end of class