Java Exception Handling

Vinnu Mukhi
May 13, 2021
  1. 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

--

--

Vinnu Mukhi
0 Followers

I'm a Computer Science Student from Indore (India) and I'm Sharing all my learnings here.