Chapter 4
Chapter 4
Chapter 4:
Q1] Write a program to accept password from user and throw ‘Authentication
failure’ exception if password is incorrect.(8M)(W-15)
import java.util.*;
String pass;
Wrong_password(String a){
pass=a;
return("authentication failure"+pass);
class passworddemo
{
String pass,pass1;
pass=s1.next();
pass1=s1.next();
if(!(pass.compareTo(pass1)==0))
try
catch(Wrong_password e)
System.out.print(e);
else
System.out.print("set password="+pass);
}
Q2]
Define
an
exception called ‘No match Exception’ that is thrown when a string is not equal to
“MSBTE”. Write program. (8M) (W-16)
import java.util.*;
class No_match_Exception extends Exception{
String pass;
No_match_Exception(String str2){
pass=str2;
}
public String toString(){
return("String is Not Equals to MSBTE="+pass);
}
}
class MSBTE{
public static void main(String arg[]){
String str1="MSBTE";
String str2;
Scanner s=new Scanner(System.in);
System.out.println("Enter String :=");
str2=s.next();
if(!(str1.compareTo(str2)==0)){
try{
throw new No_match_Exception(str2);
}
catch(No_match_Exception e){
System.out.println(e);
}
}
else{
System.out.println(" String is Equals To:="+str1);
}
}
}
import java.util.*;
class Even_No_Excetion extends Exception{
int n;
Even_No_Excetion(int a){
n=a;
}
public String toString(){
return("Even Number Exception="+n);
}
}
class EvenDemo{
public static void main(String arg[]){
int no;
Scanner s=new Scanner(System.in);
System.out.println("Enter Any Integer No. :=");
no=s.nextInt();
if(no%2==0){
try{
throw new Even_No_Excetion(no);
}
catch(Even_No_Excetion e){
System.out.println(e);
}
}
else{
System.out.println(" Odd No:="+no);
}
}
}