Exception handling

1. DivideByZero Exception


 import java.util.Scanner;

public class Arr

{

public static void main(String Args[])

{

  try 

  {

  int a=20; 

  

  int b=0; 

  

  int c; 

  

  c=a/b;  

  

  System.out.println(c);

  } 

  

  catch(Exception e) 

  { 

  System.out.println("can not Divide by zero"); 

  }

  



2. ArrayOutOfIndex Exception- 


import java.util.Scanner;

public class Arr

{

public static void main(String Args[])

{

  Scanner s1 = new Scanner(System.in); 

  try 

  { 

  int st; 

  int a[] ={10,20,30,40,50}; 

 

 System.out.println("enter the position of an element");

 

  st= s1.nextInt(); 

  

  System.out.println(a[st]);

  } 

  

  catch(Exception e) 

  { 

  System.out.println("Index out of the array"); 

  }

  

}


}


Comments