Silly Try Catch question...

G

Guest

Hello I know this is extremely basic, I just want to make sure I got it right.

is:

try{}
catch{}

the same as

try{}
catch(System.Exception e){}

I mean if I am not using the reference to the exception then I should simply
use
catch{} ? or is catch(System.Exception){} somehow more restricted on what
it catches than simply catch{}?

Thanks in advance

JT.
 
C

cody

"catch(System.Exception e)"

will catch *all* .NET exceptions.

simply "catch" will also catch exceptions not based on System.Exception,
that is not CLS compliant exceptions which could be thrown by native code or
managed c++.

If you do not need a reference to the exception use
"catch(System.Exception)".

But you always should only catch the most specific exception which you are
expecting for example FormatException when you are parsing something and so
on, this makes sure you are not swallowing heavy bugs.
 
G

Guest

Thanks!

very helpful.

cody said:
"catch(System.Exception e)"

will catch *all* .NET exceptions.

simply "catch" will also catch exceptions not based on System.Exception,
that is not CLS compliant exceptions which could be thrown by native code or
managed c++.

If you do not need a reference to the exception use
"catch(System.Exception)".

But you always should only catch the most specific exception which you are
expecting for example FormatException when you are parsing something and so
on, this makes sure you are not swallowing heavy bugs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top