Java Exception Handling
May 13, 2021
- Number Format exception
public class exception {
public static void main(String[] args) {try{
int num =Integer.parseInt("Virendra");
System.out.println(num);
}
catch(NumberFormatException e){
System.out.println("Number Format Exception Please Enter Integer Value");
}
}
}Output: Number Format Exception please Enter Integer Value
2. Array Index Out of Bound Exception
public class exception {
public static void main(String[] args) {try{
int a[]= new int[5];//insert a value in Array
a[6]=24;
System.out.println(a[4]);
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("This is Array Index Out Of Bound Exception);
}
}
}Output: This is Array Index Out Of Bound Exception
3. Throw an exception
public class exception {
static void avg(){
try {
throw new ArithmeticException("Demo");
}
catch (ArithmeticException e){
System.out.println("Exception caught");
}
} public static void main(String[] args) {
avg();
}
}Output:Exception caught