Java try / catch problem
I'm new to Java. I always encounter problems with the try / catch function in the code, so I wonder if you can help me
The problem I encountered was that I wrote a try / catch, but I received an error message saying that I would never throw an exception I wrote a similar statement in another part of my whole code. It really works, so I want to know what the problem is
This 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 catch, which means that an exception will never be thrown So I would appreciate it if you could give me some insight into what is happening
Solution
You don't actually use the parameter file logfile to do anything. You only use the empty string fileline = ""; You defined it Therefore, there is no file access, so there is no possibility of FileNotFoundException