0% found this document useful (0 votes)
1K views7 pages

Chapter 4

The document contains code for 3 Java programs that demonstrate using exceptions. The first program prompts the user to enter a password and throws an AuthenticationFailure exception if the password does not match on re-entry. The second program defines a NoMatchException that is thrown if a user-entered string does not equal "MSBTE". The third program defines an EvenNumberException that is thrown if a user-entered number is even, to demonstrate custom exceptions.

Uploaded by

Raj Debadwar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views7 pages

Chapter 4

The document contains code for 3 Java programs that demonstrate using exceptions. The first program prompts the user to enter a password and throws an AuthenticationFailure exception if the password does not match on re-entry. The second program defines a NoMatchException that is thrown if a user-entered string does not equal "MSBTE". The third program defines an EvenNumberException that is thrown if a user-entered number is even, to demonstrate custom exceptions.

Uploaded by

Raj Debadwar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Java Programs

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.*;

class Wrong_password extends Exception{

String pass;

Wrong_password(String a){

pass=a;

public String toString()

return("authentication failure"+pass);

class passworddemo

public static void main(String arg[])

{
String pass,pass1;

Scanner s1=new Scanner(System.in);

System.out.println("enter the password=");

pass=s1.next();

System.out.println("Re-enter the password=");

pass1=s1.next();

if(!(pass.compareTo(pass1)==0))

try

throw new Wrong_password(pass1);

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);
}
}
}

You might also like