Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Java Java Exception Handling: Handling Checked Exceptions

Hilton D

Active Coder
I'm working on a Java application that involves file I/O, and I'm dealing with checked exceptions, specifically FileNotFoundException when attempting to read a file. I want to handle this exception gracefully in my code.

Here's a simplified version of what I'm trying to do:

Java:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class FileProcessor {

    public static void main(String[] args) {
        String fileName = "sample.txt";

        try {
            BufferedReader reader = new BufferedReader(new FileReader(fileName));
            String line;

            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }

            reader.close();
        } catch (IOException e) {
            System.err.println("An error occurred while reading the file: " + e.getMessage());
        }
    }
}

In this code, I'm attempting to read the contents of a file named "sample.txt." If the file doesn't exist or there's an I/O error, a FileNotFoundException or an IOException might occur.

I want to improve the exception handling in my code to provide more meaningful error messages and possibly take different actions based on the exception type. Could you provide a concise Java code example demonstrating how to handle these exceptions effectively while keeping the code clean and readable? Thank you for your assistance!
 
use multiple catch blocks.
A single try can have multiple catches, each for a different Exception, so for example you can catch FileNotFoundException and issue a suitable message, then catch EOFException and issue a different message, then catch IOException to mop up any remaining types. (Only he first matching catch will be executed )

ps. You should be using a try with resources (google it) to create the file reader and the buffered reader and guarantee that it will be closed properly after an Exception
 
Last edited:

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom