Wednesday, 2 September 2015

S7P2

Define an Exception called “NoMatchException” that is thrown when a string is not equal to “India”. Write a program that uses this exception.





Code:
 class NoMatchException extends Exception  
 {  
      String m;  
      NoMatchException(String message)  
      {  
           m = message;  
      }  
      void printmsg()  
      {  
           System.out.println(m);  
      }  
 }  
 class S7P2_NoMatch  
 {  
      public static void main(String args[])  
      {  
           if(args.length!=1)  
           {  
                System.out.println("\nEnter only one String argument");  
           }  
           else  
           {  
                try  
                {  
                     if(args[0].compareTo("India") == 0)  
                     {  
                          System.out.println("\nString is equal to 'India'.");  
                     }  
                     else  
                     {  
                          throw new NoMatchException("Arguments is not equal to 'India'.");  
                     }  
                }  
                catch(NoMatchException e)  
                {  
                     System.out.println("\nException Caught: ");  
                     e.printmsg();  
                }  
           }  
      }  
 }  

Output:

No comments:

Post a Comment