Silly Try Catch question...

  • Thread starter Thread starter Guest
  • Start date Start date
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.
 
"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.
 
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.
 
Back
Top