Define an exception called ‘NotMatchexception’ that is thrown when a string is not equal to ‘India’. Write a java program that uses this exception.
Code:
import java.util.Scanner;
class NotMatchException
extends Exception
{
public NotMatchException (String str)
{
System.out.println(str);
}
}
public class StringExc
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter the string :
");
String input = scan.nextLine();
try
if(input.equalsIgnoreCase("India"))
System.out.println("String
matched !!!");
else
throw new NotMatchException
("String not matched !!!");
}
catch (NotMatchException s)
{
System.out.println(s);
}
}
}
Comments
Post a Comment