Exception Handling

DarkD

Member
Joined
Jun 16, 2014
Messages
16
Programming Experience
Beginner
I am trying to implement exceptions properly, but I'm having a little trouble understanding a few things. Let me see if I have this straight so far

1) when an exception occurs I let it run up the stack for the most part
2) I catch an exception when I believe that I can recover from it (user enters bad input type of exception so ask for different input.)
3) use finally when you want to dispose of resources. It will still run whether the exception occurs or not. So use catches instead if you are only disposing of something on failure.
4) I should use Exception.Message() for displaying to the user, and Exception.ToString for input into a log file.
5) I should use an UnhandledExceptionEventHandler for logging errors instead of a try catch statement if my only purpose is logging errors in a log file.

So what I am having trouble with is

1) when I use Image.FromFile, I try that and if I catch something, do I still need to dispose of the Image regardless of the fact that an error occured? IE
C#:
            try            {
                LBImage = Image.FromFile(fName);
            }
            catch (ArgumentException e)
            {
                LBImage.Dispose();
                throw e;
            }
2)nvm figured this one out on my own through a stupid amount of digging. Someone needs to add UnhandledExceptionEventHandler to the MSDN for try catches... That would have saved me so much time...
3)nvm figured this one out too.. stupid users on other forums gave bad advice and confused the issue.
4) What sort of messages should I use when I throw an exception for lets say a Null paramater that was passed to my method? I've been doing throw new ArgumentNullException("ClassName->Constructor: Type varName was null");
5) I have a bunch of classes being made in my main class and each of them needs to call dispose before excution of my program ends from an exception. Do I stick my entire Main into a try{}finally{} statement so that I always call dispose? Seems odd to stick my whole program in one...
 
Last edited:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,272
Location
Sydney, Australia
Programming Experience
10+
when I use Image.FromFile, I try that and if I catch something, do I still need to dispose of the Image regardless of the fact that an error occured? IE
C#:
            try            {
                LBImage = Image.FromFile(fName);
            }
            catch (ArgumentException e)
            {
                LBImage.Dispose();
                throw e;
            }

If an exception is thrown when you call Image.FromFile then no Image object is created so you have nothing to dispose.

By the way, if you ever rethrow an exception, never do it explicitly like that but rather do it implicitly, i.e. don't use 'throw e' in that case but rather just use 'throw'. If you rethrow the exception explicitly then you truncate the stack trace so it appears to have originated in your method.
 
Top Bottom