Zombian
Zombian

Reputation: 1435

Why am I catching the exception

I am running the following code to try and read from a text file. I am fairly new to java and have been practicing by trying to create projects for myself. The following code is slightly modified from what I originally found to try and read a text file but for some reason it catching the exception every time. The text file that it is trying to read from only says "hello world". I assume it must not be finding the text file. I put it in the same folder as the source code and it appears in the source packages (I'm using netbeans btw). It probably just needs to be imported differently but I can't find any further info on it. In case my code is relevant here it is below.

package stats.practice;

import java.io.*;
import java.util.Scanner;

public final class TextCompare {

    String NewString;

    public static void main() {
        try {
            BufferedReader in = new BufferedReader(new FileReader("hello.txt"));
            String str;
            while ((str = in.readLine()) != null) {
                System.out.println(str);
            }
            in.close();
        } catch (IOException e) {
        } 
        System.out.println("Error");
    }
}

Upvotes: 1

Views: 84

Answers (4)

Matt Ball
Matt Ball

Reputation: 359776

The closing brace in the catch block is misplaced. Move it to be below the System.out.println("Error");.

public static void main(String[] args) {
    try {
        BufferedReader in = new BufferedReader(new FileReader("hello.txt"));
        String str;
        while ((str = in.readLine()) != null) {
            System.out.println(str);
        }
        in.close();
    } catch (IOException e) { // <-- from here
        System.out.println("Error");
        // or even better
        e.printStackTrace();
    } // <-- to here
}

As a matter of defensive programming (pre-Java 7 at least) you should always close resources in a finally block:

public static void main(String[] args) {
    BufferedReader in = null;
    try {
        in = new BufferedReader(new FileReader("hello.txt"));
        String str;
        while ((str = in.readLine()) != null) {
            System.out.println(str);
        }
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (Exception e) {}
        }

        // or if you're using Google Guava, it's much cleaner:
        Closeables.closeQuietly(in);
    }
}

If you are using Java 7, you can take advantage of automatic resource management via try-with-resources:

public static void main(String[] args) {
    try (BufferedReader in = new BufferedReader(new FileReader("hello.txt"))) {
        String str;
        while ((str = in.readLine()) != null) {
            System.out.println(str);
        }
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Upvotes: 7

havexz
havexz

Reputation: 9590

First step, replace below code

catch (IOException e){}

with

catch ( IOException e) { e.printStackTrace(); }

and also replace

main()

with

main(String[] args)

This will tell you the exact reason. and then you have to solve the actual reason.

Now for Netbeans, the file hello.txt has to be in your Netbeans project. like

<project_dir>
    |
    -->hello.txt
    -->build
    -->src

Upvotes: 2

Francis Upton IV
Francis Upton IV

Reputation: 19443

You have an empty catch block which is almost always a bad idea. Try putting this there:

... catch (IOException ex) {
  ex.printStackTrace();
}

And you should quickly see what's going on.

Upvotes: 1

Cameron S
Cameron S

Reputation: 2301

It isn't necessarily catching the exception every time. Your System.out.println("Error"); statement is outside of the catch block. Therefore, it is executed every time the program executes.

To fix this, move it within the braces (catch (IOException e) {System.out.println("Error");})

Upvotes: 3

Related Questions