Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Saturday 2 February 2019

SECOND WEEK TERM WORK Program 2

2) Write a Java Program that takes two strings as command-line arguments and checks whether
both command-line arguments are equal or not. If they are equal then print proper message;
otherwise generate a used-defined exception StringNotEqualException.

package exception2;
class StringNotEqualException extends Exception
{
public StringNotEqualException(String s)
{
super(s);
}
}

public class ExceptionTest  {
public static void main(String[] args) {
if(args.length!=2)
{
System.out.println("Enter two strings only");
}
else {
try {
boolean num=args[0].equals(args[1]);
if(num!=true)
{
throw new StringNotEqualException("String Not Equal");
}
else {
System.out.println("Equal");
}
}
catch(Exception e)
{
System.out.println("Error");
System.out.println(e.getMessage());
}
}
}
}

0 comments:

Post a Comment