Is there a better way to handle exceptions when using file handling? maybe catch more specific exceptions like FileNotFound? What do you recommend? I'm asking this since when I am using my executable file for my application on another computer, I am getting the message box (an exception is being caught) and I can't know what it is! Also this exception isn't showing on each computer I have this application so it's more difficult to figure out!

Are you employing any kind of logging that provides a log file with full exception/stack trace information? Something like Log4Net?
–
Chris SinclairDec 11 '12 at 21:25

@Chris Sinclair no I'm not using any logging
–
BerniceDec 11 '12 at 21:31

If you define a variable for each exception type, you get access to the message; for an IOException, that message will indicate the exact nature of the problem (such as an expected file not existing, corrupted file, device failure, etc). In addition, IOException is the base class for exceptions of more specific types, which you can catch and handle differently. Lastly, I would specifically catch SecurityExceptions as well; these are thrown when the user doesn't have the proper file permissions to do whatever you were trying to do.
–
KeithSDec 11 '12 at 21:35

@studentProgrammer: I would definitely look into implementing it, especially if you're trying to debug failures on multiple machines. Make sure that any unhandled exceptions in your application log full information out and you'll usually see exactly why. So for example, in this case, you might see a FileNotFoundException or a PathTooLongException or a DirectoryNotFoundException (all IOExceptions btw) without having to specifically catch all of them. Really, it covers the exceptional cases that you had not accounted for and gives you information critical to debugging it.
–
Chris SinclairDec 11 '12 at 21:35

@KeithS Yes it's a good idea to catch security exceptions, thanks! Do you think that in my case, (since I am saving into the temp folder of the computer the application is working on), this could be a security issue? Chris I think I'm going to use logging if I have the chance of reading on them, since I only used them once and I had no idea what I was doing! I'll research a bit on logging. I think it's worth it! Thanks!
–
BerniceDec 11 '12 at 21:46

As a general rule, you usually should not catch every exception (by writing catch(Exception)), but only those, that you can really handle at this particular point.

Take a look at the documentation and see which exceptions can be thrown by all methods used in your saving code. Think about what they really mean in case of your specific algorithm and how you could translate the exceptions into user-friendly error messages.

For example, UnauthorizedAccessException and FileNotFoundException are very different things. The user does not understand what's wrong if you just write "An error occured".

The exception based workflow management is a suggested guideline, if not the only possible, in case of dealing with IO operations, so files too. Very often there is no other way to handle your program workflow, if not by expected exceptions handling when you're dealing with IO artifacts.

In your case, I would add catch on all expected exceptions, and for others just will leave to go up to the stack.

But if the file must exist prior to an operation I would alway do a File.Exists(...)...
–
Mario The SpoonDec 11 '12 at 21:28

@MarioTheSpoon That's can be worthwhile, but still introduces a race condition whereby after the File.Exists call but before the file opening, the file is removed by whatever process. Hence the caught exceptions are the only sure-fire way of knowing what's going on. Sometimes depending on your context, this is OK to do a File.Exists and assume that the file won't be modified before your subsequent read, but not always.
–
Chris SinclairDec 11 '12 at 21:31

@MarioTheSpoon: depends how program works, it's much easier and clearer just manage it inside simple try/catch..
–
TigranDec 11 '12 at 21:32