0% found this document useful (0 votes)
22 views2 pages

Java PR 23&24&25

The document discusses exception handling in Java programs. It shows how to catch and handle exceptions using try-catch blocks and the finally block. It also shows how to create a custom exception class that extends the Exception class.

Uploaded by

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

Java PR 23&24&25

The document discusses exception handling in Java programs. It shows how to catch and handle exceptions using try-catch blocks and the finally block. It also shows how to create a custom exception class that extends the Exception class.

Uploaded by

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

Program:

public class ExceptionDemo {


public static void main(String[] args) {
int a, b, c = 0;
try {
a = Integer.parseInt(args[0]);
b = Integer.parseInt(args[1]);
c = a / b;
System.out.println("Ans: " + c);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Please Pass some argument");
} catch (ArithmeticException e) {
System.out.println("Can't divide number by zero");
} catch (NumberFormatException e) {
System.out.println("Please enter the number");
} finally {
System.out.println("Finally: I am executed Always");
}
}
}
Output:
java ExceptionDemo 27 3
Ans: 9
Finally: I am executed Always

Program:
import java.util.Scanner;

class AuthenticationFailure extends Exception {


public AuthenticationFailure() {
System.out.println("Invalid Password");
}
}

public class NoMatchexception {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
try {
String s = "ABC@123";
String s1;
System.out.print("Enter your Password: ");
s1 = sc.nextLine();
if (s1.equals(s)) {
System.out.println("Login Successully");
} else {
throw new AuthenticationFailure();
}
} catch (Exception e) {
System.out.println(e);
}
}
}
Output:
Enter your Password: ABC@123
Login Successully

You might also like