-
printing errors
i would like to print error messages from another class for my program. i was thinking of either returning an array of errors if there are any, but then i heard about try/catch statements. i am checking if files exist. what would be the best way to go about this? i wasn't sure if i could use a try statement to check if two files do not exist... i was thinking about an if statement (which is what i'm currently using). i like how the try/catch statements give off a red error message though. and is there a way with the catch statements, that i could put them together, if there is more than one error (an array of e's [or exceptions])?
-
Re: printing errors
File has a method to check if it exists or not. Check out the File docs. There is also a FileNotFoundException that can be thrown FileNotFoundException (Java Platform SE 6)
-
Re: printing errors
would you print the error(s) to an array, or use the exceptions?
-
Re: printing errors
I would probably check if the file exists and if not then throw the FileNotFoundException. It depends though, do you want to kill the program if the file doesn't exist or do you want to create the file if it doesn't exist. I would probably stay away from the arrays.
-
Re: printing errors
i just like the arrays because they are a little more customizable (with the error messages). also, i am checking more than one file. is that a problem with exceptions, or can you combine them together?
it just seems that the standard file not found exception isn't as user-friendly.
-
Re: printing errors
You can pass a message to the FileNotFoundException constructor which will print out.
psuedo code
if Not file.exists
throw new FileNotFoundException("No file")