Java Exception Handling

/*
Write a Java program for handling the following exceptions
(i)        Arithmetic Exception          
(ii)       ArrayIndexOutOfBounds Exception
(iii)      NumberFormat Exception
(iv)      StringIndexOutOfBound Exception
*/


// Exception handling

class EH
{
     public static void main(String as[])
     {       
          int a = 5, b = 0;
          try
          {
              int d = a/b;
              System.out.println(d);             
          }
          catch(Exception e)
          {
              System.out.println(e);
          }
         
          String s = "123g";
          try
          {
              int n = Integer.parseInt(s);
              System.out.println(n);
          }
          catch(Exception e)
          {
              System.out.println(e);
          }
         
         
         
          int IA[] = new int[]{1,2,3,4,5};
          try
          {
              int x = IA[13]; 
              System.out.println(x);
          }
          catch(Exception e)
          {
              System.out.println(e);
          }
         
          String str = "HELLO JAVA";
          try
          {
              char c = str.charAt(33);
              System.out.println(c);             
          }
          catch(Exception e)
          {
              System.out.println(e);
          }
     }
}

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu