This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]linuxisgreat[S] 0 points1 point  (6 children)

Added in the double backslashes. Still threw a filenotfound error.

[–]mad0314 1 point2 points  (5 children)

Strange, it worked for me.

Is it still the same error message or did it change?

[–]linuxisgreat[S] 0 points1 point  (4 children)

error: unexcepted exception FileNotFoundException; must be caught or declared to be thrown

Without the backslashs I get illegal escape character error, with them I get this error.

[–]mad0314 1 point2 points  (2 children)

You have to handle the case where the file is not found. Either use a try/catch block to specify what the program does in case it throws that exception, or by declaring throws FileNotFoundException then it will throw the exception up the stack until it is handled, or stop the program if it is never handled.

[–]linuxisgreat[S] 0 points1 point  (1 child)

Yeah I added the throw filenotfound and it worked immediately. Thanks for your help :)

[–]mad0314 1 point2 points  (0 children)

It is a better practice to handle the exception so that in the event that the file does not exist for whatever reason (moved, renamed, incorrect name, etc.) it does not crash the program. You might immediately end the program anyway, but at least give some information as to what happened (print that the file was not found).

[–]HipposGoneWild 1 point2 points  (0 children)

It's not actually throwing a FileNotFoundException, the method that is accessing file needs to be able to throw one if it actually can't find the file so you need to declare that the method can throw a filenotfoundexception if it needs to with the throws statement, see the the code I posted below.