0% found this document useful (0 votes)
2 views

Practical_12

This document is a lab manual for Java programming focused on exception handling using try, catch, and finally blocks. It includes practical examples demonstrating how to handle exceptions, differentiate between errors and exceptions, and manually throw exceptions. Additionally, it provides sample programs for user authentication and exception propagation from the main method.

Uploaded by

Huzeefa Pathan
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)
2 views

Practical_12

This document is a lab manual for Java programming focused on exception handling using try, catch, and finally blocks. It includes practical examples demonstrating how to handle exceptions, differentiate between errors and exceptions, and manually throw exceptions. Additionally, it provides sample programs for user authentication and exception propagation from the main method.

Uploaded by

Huzeefa Pathan
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

Java Programming Practical:12 Lab Manual (Solved)

Resource required (additional)


Name of Resource Broad Specification Quantity Remark if any
Java Development Kit
JDK 8 or higher For Java compilation
(JDK)
Integrated
Development like Eclipse, VS code
Environment (IDE)
For command-line
Notepad Text editor
programs

Conclusion
In this practical, we wrote programs to implement the try, catch, and finally blocks. We learned
how to handle exceptions in Java and ensure code execution in the finally block regardless of
exceptions.

Practical related Questions


1. Demonstrate exception handling using try , catch and finally block.
class JPA {
public static void main(String args[]) {
try {
int num = 10 / 0; // This will cause ArithmeticException
} catch (ArithmeticException e) {
System.out.println("Error: Division by zero is not allowed. Its answer is infinity.");

} finally {
System.out.println("Finally block executed.");
}
}
}
2. Differentiate between error and exception in java.
Aspect Error Exception
A serious issue that occurs at A problem that occurs during
Definition runtime and is beyond user control. execution and can be handled by
the program.
Hardware failure, JVM issues, Invalid user input, division by
Causes
memory overflow, etc. zero, file not found, etc.

Jamia Polytechnic Akkalkuwa Page No:1 Prepared by: Sayyed Waliullah


Java Programming Practical:12 Lab Manual (Solved)

Handling Cannot be handled using try-catch. Can be handled using try-catch.


OutOfMemoryError, ArithmeticException,
Examples
StackOverflowError NullPointerException
Not recoverable, leads to application Recoverable, allows program
Recoverability
crash. continuation.

3. Can we throw exception manually? Illustrate with sample program.


Yes, we can throw an exception manually using the throw keyword.
Program: Manually Throwing an Exception
class ThrowDemo {
public static void main(String args[]) {
int age = 16;
if (age < 18) {
throw new ArithmeticException("Not eligible to vote");
}
System.out.println("Eligible to vote");
}
}
Brief Solution:
• The throw keyword is used to manually throw an exception.
• If the age is less than 18, an ArithmeticException is thrown.
• If the age is 18 or above, the program prints "Eligible to vote."

4. Develop a program to accept a password from the user and throw “Authentication Failure”
Exception if the password is incorrect.
import java.util.Scanner;

class MyAuthenticationException extends Exception {


MyAuthenticationException(String message) {
super(message);
}
}

class PasswordCheck {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String correctPassword = "java123"; // Predefined password

Jamia Polytechnic Akkalkuwa Page No:2 Prepared by: Sayyed Waliullah


Java Programming Practical:12 Lab Manual (Solved)

System.out.print("Enter Password: ");


String inputPassword = sc.next();

try {
if (!inputPassword.equals(correctPassword)) {
throw new MyAuthenticationException("Authentication Failure");
}
System.out.println("Login Successful!");
} catch (MyAuthenticationException e) {
System.out.println(e.getMessage());
}
}
}
5. How exception is thrown by main method?
• In Java, the main method can throw an exception using the throws keyword.
• If an exception is not handled within main(), it propagates to the JVM, which prints an error
message and terminates the program.
Program: Throwing an Exception from main()
class MainExceptionDemo {
public static void main(String args[]) throws ArithmeticException {
int num = 10 / 0; // This will cause ArithmeticException
System.out.println("This line will not execute.");
}
}
• The main method is declared with throws ArithmeticException.
• The division by zero causes an ArithmeticException.
• Since it is not handled, the JVM terminates the program and prints an error message.

Jamia Polytechnic Akkalkuwa Page No:3 Prepared by: Sayyed Waliullah

You might also like