java w 6 rec
java w 6 rec
: No: no:
16. Write a JAVA program that describes exception handling
mechanism. Aim: To write a program to implement exception
handling mechanism. Description:
Exception handling in Java is a powerful mechanism that allows
developers to manage runtime errors, ensuring that the program can
continue executing or terminate gracefully. It uses keywords like ‘try’,
‘catch’, ‘finally’,’ throw’, and throws to handle exceptions effectively.
In a typical scenario, code that might throw an exception is placed inside
a ‘try’ block. If an exception occurs, control is transferred to the
corresponding ‘catch’ block, where the exception can be handled. The
‘finally’ block can be used to execute code that must run regardless of
whether an exception occurred or not.
Key Concepts of Exception Handling in Java
Try-Catch Block: Used to handle exceptions. Code that may throw an
exception is placed in the ‘try’ block, and the handling code is in
the ‘catch’ block.
Finally Block: This block is optional and is executed after
the ‘try’ and ‘catch’ blocks, regardless of whether an exception was
thrown or not.
Throw and Throws: The ‘throw’ keyword is used to explicitly throw
an exception, while ‘throws’ is used in method signatures to declare
that a method can throw exceptions.
Checked vs Unchecked Exceptions: Checked exceptions must be
either caught or declared in the method signature, while unchecked
exceptions do not require explicit handling.
This structured approach to exception handling helps maintain the flow of
the program and provides a way to manage errors effectively.
Program code:
package week6;
import java.util.Scanner;
class WeightLimitExceeded extends Exception {
WeightLimitExceeded(int weight) {
super("Weight limit exceeded by " + Math.abs(15 - weight) + " kg");
}
}
Sample Output:
C:\23-518>javac
Main.java C:\23-
518>java Main Enter
weight: abc
Invalid input. Please enter a valid
integer. Enter weight: 16
Weight limit exceeded by 1 kg.
Result:
Hence, the program to implement exception handling mechanism has
been executed successfully.
Program code:
package week6;
import java.util.Scanner;
divide:");
try {
int num1 =
scanner.nextInt(); int
num2 =
scanner.nextInt(); int
result = num1 / num2;
System.out.println("Result: " + result);
}
catch (ArithmeticException e) {
System.out.println("Error: Division by zero is not allowed.");
}
catch (java.util.InputMismatchException e) {
System.out.println("Error: Please enter valid integers.");
}
catch (Exception e) {
System.out.println("Error: An unexpected error occurred: " +
e.getMessage());
}
finally {
scanner.close();
}
Executed Output:
C:\23-518>javac
MultipleCatchExample.java C:\23-
518>java MultipleCatchExample
Enter two integers to divide:
18
2
Result: 9
Result:
Hence, the program to illustrate multiple catch clauses has been
executed successfully.
try {
int[] arr = new int[5];
System.out.println(arr[10]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBoundsException: Index is out of
bounds.");
}
try {
Class.forName("com.example.NonExistentClass");
} catch (ClassNotFoundException e) {
System.out.println("ClassNotFoundException: The specified class
was not found.");
}
}
}
Sample Output:
C:\23-518>javac
BuiltInExceptionsExample.java C:\23-
Executed Output:
C:\23-518>javac
BuiltInExceptionsExample.java C:\23-
518>java BuiltInExceptionsExample
Result:
Hence, the program to create java built-in exceptions has been executed
successfully.
Program code:
package week6;
class InvalidAgeException extends
Exception { public
InvalidAgeException(String message) {
super(message);
}
}
try {
checkAge(200);
} catch (InvalidAgeException e) {
System.out.println("Caught exception: " + e.getMessage());
}
}
}
Sample Output:
C:\23-518>javac
UserDefinedExceptionDemo C:\23-
518>java UserDefinedExceptionDemo
Age is valid: 25
Caught exception: Age must be between 0 and
150. Caught exception: Age must be between 0
and 150.
Executed Output:
C:\23-518>javac
UserDefinedExceptionDemo C:\23-
518>java UserDefinedExceptionDemo
Age is valid: 25
Caught exception: Age must be between 0
and 150. Caught exception: Age must be