Exception Handling in Java
Exception Handling in Java - Notes
๐ธ What is an Exception?
An exception is an unwanted or unexpected event that disrupts the normal flow of a program.
๐ธ Types of Exceptions
1. Checked Exceptions – Handled during compile-time.
Examples: IOException, SQLException
2. Unchecked Exceptions – Occur at runtime.
Examples: NullPointerException, ArithmeticException
3. Errors – Serious problems like memory errors. Not meant to be handled.
Examples: OutOfMemoryError, StackOverflowError
---
✅ Exception Hierarchy
Object
↳ Throwable
↳ Exception
↳ IOException
↳ SQLException
↳ RuntimeException
↳ NullPointerException
↳ ArithmeticException
↳ Error
---
✅ Keywords in Exception Handling
Keyword Use
try Block of code to monitor for exceptions
catch Handles the exception
finally Executes always (cleanup code)
throw Used to explicitly throw an exception
throws Declares exception in method signature
---
✅ Syntax and Example
public class ExceptionDemo {
public static void main(String[] args) {
try {
int a = 10 / 0; // ArithmeticException
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero: " + e.getMessage());
} finally {
System.out.println("Cleanup done (finally block).");
}
}
}
---
✅ Using throw Keyword
public class ThrowExample {
static void validate(int age) {
if (age < 18)
throw new ArithmeticException("Not eligible to vote");
else
System.out.println("Welcome to vote");
}
public static void main(String[] args) {
validate(16);
}
}
---
✅ Using throws Keyword
import java.io.*;
public class ThrowsExample {
void readFile() throws IOException {
FileReader fr = new FileReader("abc.txt"); // May throw IOException
}
public static void main(String[] args) {
try {
new ThrowsExample().readFile();
} catch (IOException e) {
System.out.println("Handled IOException: " + e.getMessage());
}
}
}
---
✅ Multiple Catch Blocks
try {
int[] arr = new int[5];
arr[5] = 30 / 0;
} catch (ArithmeticException e) {
System.out.println("Arithmetic Exception occurred");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array index out of bounds");
} catch (Exception e) {
System.out.println("Parent Exception occurred");
}
---
✅ Common Interview Questions
1. ❓What is the difference between throw and throws?
throw is used to explicitly throw an exception.
throws is used in method signature to declare exceptions.
---
2. ❓Can we have multiple catch blocks?
Yes, Java supports multiple catch blocks to handle different exception types.
---
3. ❓What is the role of the finally block?
The finally block always executes after try/catch, used to release resources.
---
4. ❓What if exception is not handled?
If not handled, Java terminates the program abnormally and prints stack trace.
---
5. ❓Can we catch multiple exceptions in one catch block?
Yes, from Java 7 onwards:
catch (IOException | SQLException ex) {
System.out.println("Exception handled: " + ex);
}
---
6. ❓What is the difference between Checked and Unchecked exceptions?
Checked: Handled during compile-time (e.g., IOException)
Unchecked: Occur at runtime (e.g., NullPointerException)
---
Comments
Post a Comment