0% found this document useful (0 votes)
5 views2 pages

Java Exception Handling

Exception handling in Java allows programs to manage runtime errors and continue execution. It involves using keywords like try, catch, finally, throw, and throws to manage exceptions, which can be categorized as checked or unchecked. Proper exception handling is essential for creating robust applications and preventing crashes.

Uploaded by

Arun prakash
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Java Exception Handling

Exception handling in Java allows programs to manage runtime errors and continue execution. It involves using keywords like try, catch, finally, throw, and throws to manage exceptions, which can be categorized as checked or unchecked. Proper exception handling is essential for creating robust applications and preventing crashes.

Uploaded by

Arun prakash
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Exception Handling in Java – Student Notes

1. Introduction
Exception handling in Java is used to handle runtime errors. It allows the program to continue
running instead of crashing when something unexpected happens.

2. What is an Exception?
An exception is an event that disrupts the normal flow of the program. Exceptions are objects that
are created when an error occurs.
Types of Exceptions:
- Checked Exceptions (e.g., IOException, SQLException)
- Unchecked Exceptions (e.g., ArithmeticException, NullPointerException)
- Errors (e.g., OutOfMemoryError)

3. Exception Handling Keywords


Java provides 5 keywords to handle exceptions:
1. try – Code that might cause an exception. 2. catch – Code that handles the exception. 3. finally –
Code that always runs. 4. throw – Used to throw an exception. 5. throws – Declares exceptions in
method signature.

4. Basic Example
public class ExceptionDemo {
public static void main(String[] args) {
try {
int result = 10 / 0; // risky code
} catch (ArithmeticException e) {
[Link]("Error: Division by zero!");
} finally {
[Link]("Finally block executed.");
}
}
}
Output: Error: Division by zero! Finally block executed.

5. Multiple Catch Blocks


try {
String str = null;
[Link]([Link]());
} catch (NullPointerException e) {
[Link]("Null value found.");
} catch (Exception e) {
[Link]("General exception.");
}

6. Nested try Blocks


try {
try {
int arr[] = new int[5];
arr[10] = 50; // error
} catch (ArrayIndexOutOfBoundsException e) {
[Link]("Array index out of bounds.");
}
} catch (Exception e) {
[Link]("Handled outer exception.");
}

7. throw and throws


throw: Used to explicitly throw an exception.
throw new ArithmeticException("Division by zero not allowed");

throws: Declares exceptions in method signature.


void readFile() throws IOException {
FileReader fr = new FileReader("[Link]");
}

8. finally Block
The finally block always executes, whether an exception occurs or not. It is commonly used to close
resources such as files or database connections.

9. Commonly Used Exceptions


- ArithmeticException - NullPointerException - ArrayIndexOutOfBoundsException -
NumberFormatException - IOException

10. Summary
✔ Exception handling helps in writing robust programs. ✔ try-catch-finally is the main structure. ✔
throw is used to raise an exception. ✔ throws is used to declare exceptions in methods. ✔ Always
handle exceptions to avoid program crashes.

You might also like