7 - Exception Handling Program
7 - Exception Handling Program
import java.util.*;
class ArithmeticException_Demo
try
int a = 30, b = 0;
catch(ArithmeticException e) {
Output
Can't divide a number by 0
7) b) Java program to demonstrate Number Format Exception
import java.io.*;
import java.util.*;
class NumberFormat_Demo
{
public static void main(String args[])
{
try {
// "akki" is not a number
int num = Integer.parseInt ("akki") ;
System.out.println(num);
}
catch(NumberFormatException e) {
System.out.println("Number format exception");
}
}
}
Output
Number format exception
7) c) Java program to demonstrate Array Index Out Of Bound
Exception
import java.io.*;
import java.util.*;
class ArrayIndexOutOfBound_Demo
{
public static void main(String args[])
{
try{
int a[] = new int[5];
a[6] = 9; // accessing 7th element in an array of
// size 5
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println ("Array Index is Out Of Bounds");
}
}
}
Output
Array Index is Out Of Bounds