Java Try/Catch Issues

Viewed 230

I'm relatively new with Java and I've always had problems with the Try/Catch functions in the code so I was wondering if you guys could help me out.

The issue I'm having is that I've written a try/catch but I get an error message saying that the exception is never thrown. I've written a similar statement in another part of my overall code that does work fine so I wonder what the issue is with this one then.

Here is the code:

public String getMatchedLogs(String matchStr, File logFile) {
    String fileLine = "";
    try {
        Scanner ipScan = new Scanner(fileLine);
        if (fileLine.indexOf(matchStr) > -1) {
            output += fileLine;
        }else{
            System.out.println(fileLine.indexOf(""));
        }
    } catch (FileNotFoundException fnfe) {
        System.out.println(fnfe.getMessage());

    }
    return fileLine;
} //end getMatchedLogs

The error is in the catch where it says the exception is never thrown. So if you guys could give me some insight on what's going on I'd greatly appreciate it.

6 Answers

You're not actually doing anything with the parameter File logFile, you're only using the empty String fileLine = ""; you defined. Thus there's no file access going on, thus there's no chance of a FileNotFoundException.

Get in the habit of looking up methods to see if they do what you think they do.

The Scanner constructor for File objects opens a file and declares that it can throw FileNotFoundException http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#Scanner(java.io.File)

The Scanner constructor for a String scans the string itself and does not open a file, so it won't need to throw the exception http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#Scanner(java.lang.String)

By the way: Your code seems to be using the scanner to write to the String passed in ("filine"). Strings are immutable in Java, and can't change when passed into a method.

FileNotFound exception is not thrown by the Scanner scanning lines. You are not using the logFile so there is no chance that FileNotFoundException can be thrown.

The java compiler may complain if you have a catch handler that the compiler knows can never occur. (Java exception rules tend to be pretty strict compared to newer languages such as Scala)

You are catching a specific type of exception which is not getting thrown here.

So you should try:

catch (Exception e) {}

to catch any kind of exception.

If any Exception other than FileNotFoundException not be able to catch in your case. it is better to use as follows..

     try {
        try {
            Scanner ipScan = new Scanner(fileLine);
            if (fileLine.indexOf(matchStr) > -1) {
                output += fileLine;
            } else {
                System.out.println(fileLine.indexOf(""));
            }
        } catch (FileNotFoundException fnfe) {
            System.out.println(fnfe.getMessage());
        }
    } catch (Exception e) {
        // error log here
    }
Related