0% found this document useful (0 votes)
8 views3 pages

JPR Manual Answers Experiment 12

The document presents two Java programs demonstrating exception handling. The first program uses try, catch, and finally blocks to handle various exceptions such as ArrayIndexOutOfBoundsException, NullPointerException, and ArithmeticException. The second program prompts the user for a password and throws a custom AuthenticationFailureException if the password is incorrect.

Uploaded by

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

JPR Manual Answers Experiment 12

The document presents two Java programs demonstrating exception handling. The first program uses try, catch, and finally blocks to handle various exceptions such as ArrayIndexOutOfBoundsException, NullPointerException, and ArithmeticException. The second program prompts the user for a password and throws a custom AuthenticationFailureException if the password is incorrect.

Uploaded by

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

Practical No.

: 12

Program Statement 1: Demonstrate exception handling using try, catch and finally block.

Source Code:

public class Ehd


{
public static void main(String[] args)
{
int[] numbers={1,2,3};
String str=null;

try
{
int result=numbers[5];
System.out.println("Result:"+result);

int length=str.length();
System.out.println("Length:"+length);

int divisionResult=10/0;
System.out.println("Division Result:"+divisionResult);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Caught
ArrayIndexOutOfBoundException:"+e.getMessage());
}
catch(NullPointerException e)
{
System.out.println("Caught
NullPointerException:"+e.getMessage());
}
catch(ArithmeticException e)
{
System.out.println("Caught
ArithmeticException:"+e.getMessage());
}
finally
{
System.out.println("Finally block always executes.");
}

System.out.println("Program continues after try-catch-finally.");


}
}
Output:

Program Statement 2: WAP to accept a password from the user and throw “Authentication
Failure” Exception if the password is incorrect.

Source Code:
import java.util.Scanner;

class AuthenticationFailureException extends Exception


{
public AuthenticationFailureException(String message)
{
super(message);
}
}

public class Afe


{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
String pass="Pass@123";

try
{
System.out.println("Enter password:");
String epass=s.nextLine();

if(!epass.equals(pass))
{
throw new AuthenticationFailureException("Incorrect
password");
}

System.out.println("Authentication successful.");
}
catch(AuthenticationFailureException e)
{
System.out.println("Authentication failed:"+e.getMessage());
}
}
}

Output:

You might also like