Skip to main content

The string is not equal to Another String then throw custom exception "no match exception" in JAVA.

The custom exception called "no match exception" that is thrown when a string is not equal to "internet" & this string is providing through command line argument.
CODING:
import java.util.*; //To use Scanner Class & use only import java.util.Scanner;
import java.lang.*; //To use equalsIgnoreCase  Method & also use only java.lang.String.equalsIgnoreCase()
class EqString
{
 public static void main(String[] args)
  {
    Scanner s=new Scanner(System.in);
     try
      {
       System.out.print("Enter 'internet' String : ");
        String name=s.nextLine();
         if(name.equalsIgnoreCase("internet"))
          System.out.print(name + " is equal to 'internet' ");
         else
          throw new NoMatchException(name);
      }
     catch(NoMatchException e1)
      {
        System.out.print("Error : " + e1.getMessage());
      }
  }
}
class NoMatchException extends Exception //custom exception "NoMatchException"
 {
  NoMatchException(String msg)
   {
    super(msg + " is not equal to 'internet' ");
   }
}

Comments