JAVA EXCEPTION HANDLING LAB
Q1. Create a class named InputSafeCalculator. Implement a static method calculate
Ratio(String num1Str, String num2Str) that returns a String. Inside the method, use a single
try block to:
Convert num1Str and num2Str to integers using [Link]().
Calculate the integer division of the first number by the second.
Return the result as a String.
Implement a multi-catch block to handle both NumberFormatException (if [Link]()
fails) and ArithmeticException (if the divisor is 0). In the catch block, print an error message
specifying the type of error and return "Error: Invalid operation."Include a finally block that
prints: "Cleanup complete: Ratio calculation finished."In the main method, test the function
with:
Valid input (e.g., "10", "2").
NumberFormatException input (e.g., "ten", "2").
ArithmeticException input (e.g., "10", "0").
SOL: package ExceptionHandlingLab;
import [Link];
public class InputSafeCalculator {
public static String calculateRatio(String num1Str, String num2Str) {
try {
int num1 = [Link](num1Str);
int num2 = [Link](num2Str);
int result = num1 / num2;
return [Link](result);
} catch (NumberFormatException | ArithmeticException e) {
[Link]("Error: " + [Link]().getSimpleName() + " - " + [Link]());
return "Error: Invalid operation.";
} finally {
[Link]("Cleanup complete: Ratio calculation finished.");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
String input1 = [Link]();
[Link]("Enter second number: ");
String input2 = [Link]();
String result = calculateRatio(input1, input2);
[Link]("Result: " + result);
[Link]();
OUTPUTS:
a) Enter first number: 10
Enter second number: 2
Cleanup complete: Ratio calculation finished.
Result: 5
b) Enter first number: ten
Enter second number: 2
Error: NumberFormatException - For input string: "ten"
Cleanup complete: Ratio calculation finished.
Result: Error: Invalid operation.
c) Enter first number: 10
Enter second number: 0
Error: ArithmeticException - / by zero
Cleanup complete: Ratio calculation finished.
Result: Error: Invalid operation.
Q2. Create a class named SecurityManager.
Implement a static method checkPassword(String password):
It must declare throws Exception in its signature.
If the password is null or empty, use throw new Exception("Authentication failure:
Password cannot be empty.").
If the password is valid, print "Password check successful."
Implement a static method processData(String password):
It must call checkPassword(password).
Wrap the call in a try-catch block to handle the declared Exception.
If the Exception is caught, the method must print a log message
If no exception occurs, print "Data successfully processed."
In the main method, call processData() twice:
once with a valid password ("secret"),
and once with an invalid password (null).
Wrap the calls in a try-catch block to catch
SOL: package ExceptionHandlingLab;
import [Link];
public class SecurityManager {
public static void checkPassword(String password) throws Exception {
if (password == null || [Link]()) {
throw new Exception("Authentication failure: Password cannot be empty.");
if () {
throw new Exception("Authentication failure: Incorrect password.");
[Link]("Password check successful.");
}
public static void processData(String password) {
try {
checkPassword(password);
[Link]("Data successfully processed.");
} catch (Exception e) {
[Link]("Log: Exception caught in processData - " + [Link]());
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter password: ");
String inputPassword = [Link]();
processData(inputPassword);
[Link]();
OUTPUTS:
a) Enter password: secret
Password check successful.
Data successfully processed.
b) Enter password:
Log: Exception caught in processData - Authentication failure: Password cannot be empty.