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
  • Q1: Password Authentication Program
  • Q2: Define No Match Exception
  • Q3: Even Number Exception

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 [Link].*;

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([Link]);

[Link]("enter the password=");

pass=[Link]();

[Link]("Re-enter the password=");

pass1=[Link]();

if(!([Link](pass1)==0))

try

throw new Wrong_password(pass1);

catch(Wrong_password e)

[Link](e);

else

[Link]("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 [Link].*;
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([Link]);
[Link]("Enter String :=");
str2=[Link]();
if(!([Link](str2)==0)){
try{
throw new No_match_Exception(str2);
}
catch(No_match_Exception e){
[Link](e);
}
}
else{
[Link](" String is Equals To:="+str1);
}
}
}
import [Link].*;
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([Link]);
[Link]("Enter Any Integer No. :=");
no=[Link]();
if(no%2==0){
try{
throw new Even_No_Excetion(no);
}
catch(Even_No_Excetion e){
[Link](e);
}
}
else{
[Link](" Odd No:="+no);
}
}
}

You might also like