Unit Four
Unit Four
1 Exception Types
In Java, exceptions are events that disrupt the normal flow of a program. They are categorized
as:
1. Checked Exceptions: These are exceptions checked at compile time. Examples
include IOException, SQLException, and FileNotFoundException.
o Example: Trying to open a file that doesn’t exist will throw a
FileNotFoundException.
Example:
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class CheckedExceptionExample {
public static void main(String[] args) {
try {
File file = new File("nonexistentfile.txt");
FileReader fr = new FileReader(file); // May throw FileNotFoundException
} catch (IOException e) {
System.out.println("File not found: " + e.getMessage());
}
}
}
2. Unchecked Exceptions (Runtime Exceptions): These exceptions occur during the
runtime and are not checked at compile time. Examples include
NullPointerException, ArrayIndexOutOfBoundsException, and ArithmeticException.
o Example: Dividing by zero will throw an ArithmeticException.
Example:
public class UncheckedExceptionExample {
public static void main(String[] args) {
int a = 10, b = 0;
try {
int result = a / b; // Throws ArithmeticException
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero: " + e.getMessage());
}
}
}
3. Error: Errors are serious issues that a program should not try to handle, such as
OutOfMemoryError, StackOverflowError.
o Example: Running a deep recursion may throw a StackOverflowError.
4.2 Using Try Catch and Multiple Catch - Nested try, throw, throws, and finally
Try-Catch Block
A try block is used to enclose the code that might throw an exception. The catch block
catches and handles the exception.
Example: Catching an ArithmeticException and ArrayIndexOutOfBoundsException.
Example:
public class MultipleCatchExample {
public static void main(String[] args) {
try {
int arr[] = {1, 2, 3};
System.out.println(arr[5]); // Throws ArrayIndexOutOfBoundsException
int result = 10 / 0; // Throws ArithmeticException
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array index is out of bounds: " + e.getMessage());
} catch (ArithmeticException e) {
System.out.println("Arithmetic error: " + e.getMessage());
}
}
}
Nested Try Block
A try block inside another try block is called a nested try block.
Example: Nested try to handle multiple scenarios.
Example:
public class NestedTryExample {
public static void main(String[] args) {
try {
try {
int data = 50 / 0; // Throws ArithmeticException
} catch (ArithmeticException e) {
System.out.println("Arithmetic error: " + e.getMessage());
}
int arr[] = new int[5];
arr[7] = 10; // Throws ArrayIndexOutOfBoundsException
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array index error: " + e.getMessage());
}
}
}
Throw and Throws
Throw: Manually throw an exception using the throw keyword.
Throws: Indicates that a method may throw exceptions.
Example: Using throw and throws.
Example:
public class ThrowThrowsExample {
public static void divide(int a, int b) throws ArithmeticException {
if (b == 0) {
throw new ArithmeticException("Cannot divide by zero");
} else {
System.out.println("Result: " + (a / b));
}
}
File Handling
File handling in Java allows you to read and write data to files.
Summary
Byte Stream: Used for handling raw binary data. Classes like FileInputStream and
FileOutputStream help read/write bytes to/from files.
Character Stream: Designed for handling text files. Classes like FileReader and
FileWriter help read/write characters to/from files.
File Operations:
o Creating Files: Using the File class to create new files.
o Reading Files: Using FileInputStream for byte streams and FileReader for
character streams.
o Writing Files: Using FileOutputStream for byte streams and FileWriter for
character streams.