Module -10 Exceptions and File Handling
2026-05-07 13:00
Update: 05/18/2026
Tags: #java
Author: Duke Hsu
1.0 Exceptions / Error Handling
Exception handling lets you catch and handle errors during runtime, so your program doesn't crash.
Error management / Error handling
1.1 try and catch
The try statement allows you to define a block of code to be tested for errors while it is being executed .
The catch statement allows you to define a block of code to be executed, if an error occurs in the try block .
Therefor , try and catch keywords come in pairs.
Syntax:
1 2 3 4 5 6 7 8 | |
Example code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
1.2 finally Keyword
The finally statement lets you execute code, after try.....catch, regardless of the result
The finally block code always will executed after try.....catch
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
1.3 throw Keyword
The throw statement allows you create a customer error.
The throw statement is used together with an exception type .
Code Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
Output:
1 2 3 4 5 6 7 8 9 10 | |
1.4 throws Keyword
It warns callers that they must prepare for specific checked exceptions.
Its primary use is for checked exceptions, which the java complier requires you to either handle with try-catch or declare with throws
Code Example
1 2 3 4 5 6 7 8 9 10 11 12 | |
throw vs throws
| Feature | throw | throws |
|---|---|---|
| Purpose | Used to explicityly trigger an exception | Used to declare that a method might throw exceptions |
| Locaton | Inside the method body | In the method signature |
| Quanity | Followed by a single instance | Follorwed by a comma-separated list of classes. |
1.5 Checked Exceptions and Unchecked Exceptions
Checked Exceptions
How to handle: You must either catch it with a try-catch block it in the method signature using the throws keyword
- IOException
- FileNotFoundException
- SQLException
- ClassNotFoundException
Unchecked Exceptions
Also known as Runtime Exceptions
How to Handle: While not mandatory, you can still catch them. However, the best pracitce is to fix the underlying code logic to prevent them from occurring.
- ArrayIndexOutOfBoundsException
- NumberFormatException
- ArithmetucException
- OutOfMemoryError
| Feature | Checked Exception | Unchecked Exception |
|---|---|---|
| Check Time | Compile-time | Runtime |
| Handling | Mandatory: Must use try-catch or throws |
Optional: Not required by complier |
| Hierarchy | Subclasses of Exception (except RuntimeException) |
Subclasses of RuntimeException or error |
| Common Cause | External factors (network, file system, DB) | Programing errors (logical, nulls, math) |
1.6 Custom Exception
A user-defined custom exception is an exception class created by the programmer to represent application-specific or business-specific error scenarios.
Examples of User-defined Exception: - Invalid bank transaction - Insufficient balance - Age not eligible for registration - Invalid login attempt
Code Example
Problem without custom exceptions
1 2 3 4 5 6 7 8 9 | |
Issues with this Approach:
- The error message is unclear
- No meaningful exception is thrown
- Difficult to debug in large applications.
- Business logic and error handling are mixed
Create a User-Defined Custom Exception
- Create a class extending Exception or RuntimeException
- Provide constructors with custom messages
- Optional: Add extra fields or methods
Checked Custom Exception
- Create a class extending Exception
1 2 3 4 5 6 7 | |
- Using the custom exception
1 2 3 4 5 6 7 8 9 | |
1 2 3 4 5 6 7 8 | |
2.0 Errors and Exception Types
The table below shows a list of common Error and Exception types in Java;
Exceptions Table
| Error/Exception | Description |
|---|---|
| ArithmeticError | Occurs when a numeric calculation goes wrong |
| ArrayIndexOutOfBoundsException | Occurs when trying to access an index number that does not exist in an array |
| ClassNotFoundException | Occurs when trying to access a class that does not exist |
| FileNotFoundException | Occurs when a file cannot be accessed |
| InputMissmatchException | Occurs when entering wrong input(e.g. text in a numerical input) |
| IOException | Occurs when an input or output operation fails |
| NullPointerException | Occurs when trying to access an object refrece that is null |
| NumberFormatException | Occurs when it is not possible to convert a specified string to a numeric type |
| StringIndexOutOfBoundsException | Occurs when trying to access a character in a String that does not exist |
3.0 Java File Handling
The File class from the java.io package, allows us to work with files.
To use the File class, create an object of the class, and specify the filename or directory name.
**Code Example: **
1 2 | |
3.1 Create Files
In Java, you can create a new file with the createNewFile() method from the File class.
This method returns:
true- if the file was created successfullyfalse- if the file already exists
Tips
Note that the method is enclosed in a try....catch block. This is necessary , because it throws an IOException if an error occurs (if the file cannot be created for some reason)
Code Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
The createNewFile() method only create an empty file. it does not add any content inside.
3.2 Write Files
In Java, you can use FileWriter class with its write() method to create and write some text into a file.
Warning
When you are writer content done, you should close the writer with the close() method
Code Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
3.3 Read Files
Open a file and get the data / content inside the file
Code Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
Code Explanation:
- File > opens the file
- Scanner > reads the content
- While(reader. hasNextLine()) > reads line by line
- close() > closes the reader
3.4 Delete Files
Remove a file permanently from the storage.
Code Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
3.5 Get File Information
To get more information about a file, use any of the File methods
**Code Example: **
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
References
https://www.w3schools.com/java/java_try_catch.asp
https://www.w3schools.com/java/java_ref_errors.asp
https://www.w3schools.com/java/java_files.asp
https://www.geeksforgeeks.org/java/user-defined-custom-exception-in-java/
https://docs.oracle.com/javase/tutorial/essential/io/file.html