Experiment No: - 04 Date: - …………………………
Name: -Rajesh kumar Roll No: 2401320100275
Aim
To implement error handling techniques in Java using Exception Handling.
Objective
To understand how exceptions occur in Java
To learn how to handle exceptions using try, catch, finally, throw, and throws
To make programs robust and prevent abnormal termination
Theory
Exception handling in Java is a mechanism to handle runtime errors so that the normal flow of the
program can be maintained.
An exception is an unwanted or unexpected event that occurs during the execution of a program.
Types of Exceptions
1. Checked Exceptions (Compile-time)
Example: IOException, SQLException
2. Unchecked Exceptions (Runtime)
Example: ArithmeticException, NullPointerException
Keywords Used
try → Contains code that may cause exception
catch → Handles the exception
finally → Executes always (cleanup code)
throw → Used to explicitly throw an exception
throws → Declares exceptions
Faculty name: - Teacher’s Sign:
Experiment No: - 04 Date: - …………………………
Name: -Rajesh kumar Roll No: 2401320100275
Program 1: Basic Exception Handling (Division by Zero):
import [Link].*;
public class Exception1 {
public static void main(String[] args) {
int a, b, result;
Scanner sc = new Scanner([Link]);
[Link]("Enter two numbers: ");
a = [Link]();
b = [Link]();
try {
result = a / b;
[Link]("Result: " + result);
} catch (ArithmeticException e) {
[Link]("Error: Cannot divide by zero!");
}
[Link]("Program continues...");
}
}
Faculty name: - Teacher’s Sign:
Experiment No: - 04 Date: - …………………………
Name: -Rajesh kumar Roll No: 2401320100275
import [Link];
public class Exception2 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int a, b, ans;
[Link]("Enter two number: - ");
a = [Link]();
b = [Link]();
try {
ans = a/b;
[Link]("The result of %d / %d is %d\n", a, b, ans);
int arr[] = new int[5];
arr[10] = 50;
} catch (ArithmeticException e) {
[Link]("Arithmetic Exception");
} catch (ArrayIndexOutOfBoundsException e) {
[Link]("Array Index Out of Bounds Exception");
} catch (Exception e) {
[Link]("General Exception");
Faculty name: - Teacher’s Sign:
Experiment No: - 04 Date: - …………………………
Name: -Rajesh kumar Roll No: 2401320100275
Program 3: Using finally Block
import [Link];
public class Exception3 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int a, b, result;
[Link]("Enter the two number: - ");
a = [Link]();
b = [Link]();
try {
result = a / b;
[Link]("The the result of %d / %d is %d \n",
a,b,result);
} catch (ArithmeticException e) {
[Link]("Handled exception");
} finally {
[Link]("This block always executes");
}
[Link]();
}
}
Faculty name: - Teacher’s Sign:
Experiment No: - 04 Date: - …………………………
Name: -Rajesh kumar Roll No: 2401320100275
Program 4: Using throw Keyword
import [Link];
public class Exception4 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the age of voter: - ");
int age = [Link]();
if (age < 18) {
throw new ArithmeticException("Not eligible to vote");
} else {
[Link]("Eligible to vote");
}
}
}
Faculty name: - Teacher’s Sign:
Experiment No: - 04 Date: - …………………………
Name: -Rajesh kumar Roll No: 2401320100275
Program 5: Using throws Keyword
import [Link];
public class Exception5 {
static int divide(int a, int b) throws ArithmeticException {
return a / b;
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int a, b, result;
[Link]("Enter two numbers: ");
a = [Link]();
b = [Link]();
try {
result = divide(a, b); // method call
[Link]("Result = " + result);
} catch (ArithmeticException e) {
[Link]("Exception caught: Division by zero is not allowed.");
}
[Link]("Program continues...");
}
}
Faculty name: - Teacher’s Sign:
Experiment No: - 04 Date: - …………………………
Name: -Rajesh kumar Roll No: 2401320100275
Conclusion: -
Exception handling in Java helps in handling runtime errors efficiently and ensures that the program
runs smoothly without termination. It improves program reliability and readability.
Faculty name: - Teacher’s Sign: