Link to home
Start Free TrialLog in
Avatar of Mr_Fulano
Mr_FulanoFlag for United States of America

asked on

Question regarding Try, Catch, Finally

I have some C# code in VS2010 that I've been working on. The code contains a Try, Catch, Finally block. When I test my code using the  Console.Writeline() method, I can see Exception error messages in the Output window, which can obviously be turned off if I don't want to display the exception messages.

So, my question is this...are those error messages the messages that are generated from my Catch block or are those exception error message in addition to the ones being trapped in my Catch block?

My concern is that the Try, Catch, Finally block is not trapping all the exceptions. If that were the case, would the application abort or would it just display error messages in the Output window?

I hope this question is not too confusing, but I'm a bit puzzled myself.

Thanks for your help,
Fulano
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

By default, Try... Catch will capture all the exceptions, but it also depends on what kind of exceptions you trying to "catch" with, such as IOException will only catch the exceptions relating to Input/Output operations, FileNotFoundException will only be able to handle a case when the file referring to is not found, etc.

to capture all the exceptions, you can try:
try
        {            
            // your codes here...
        }
        catch (Exception e)
 {
     // handle exception here...
}

Open in new window


try-catch (C# Reference)
https://msdn.microsoft.com/en-sg/library/0yd65esw.aspx

try-catch-finally (C# Reference)
https://msdn.microsoft.com/en-us/library/dszsf989.aspx
>>from my Catch block or are those exception error message in addition to the ones being trapped in my Catch block?

They come from your WriteLine statement, in other words they come from where you code them to come from.
SOLUTION
Avatar of Athar Syed
Athar Syed
Flag of Kuwait image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@kaufmed - good point, they never crossed my mind.
Avatar of Mr_Fulano

ASKER

All the responses were fantastic. -- I was able to get a complete solution to my question from both athar13 and from Kaufmed, who both did a great job of explaining the Try/Catch statement, and the "first chance" exceptions .... my exception messages are ALL "first chance" exceptions. (Please see below)

-- A first chance exception of type 'System.NullReferenceException' occurred in MetaDatareader_with_Loop.exe

-- A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll

Per the link provided by Kaufmed, I now know the following:

"...For applications / components which handle exceptions gracefully, first chance exception messages let the developer know that an exceptional situation was encountered and was handled.'

From athar13's post, I now know that I can catch multiple exceptions, which I never knew!!! Very useful indeed.

Thank you both and I have split the points.

Fulano
Excellent!!!!