Both cases do not create an Exception instance. The instance exists, and
was created earlier by calling new ArgumentException( ... ) for example.
Only the "new" keyword really creates objects.
The only difference in the code you show is that, in one case, you get a
reference on the Exception instance (ex), and in the other case you
don't. If you have an instance, and don't use it, you will get a warning
when you compile. That's why you sometimes prefer to use catch (
Exception ), without the "ex".
Such case can occur, for example, when you want to do some clean up when
an error happen:
StreamWriter swr = null;
// do something
catch ( Exception )
// This code is executed if there is an error or not
if ( swr != null )
In the example above, I don't do anything with the exception, thus I use
( Exception ) without ex. If I wanted to do some logging, or maybe wrap
the Exception in another own one, I would do:
catch ( Exception ex )
Trace.WriteLine( ex.Message );
MyOwnException myEx = new MyOwnException( "Error", ex );
In that case, I'd need the reference to ex, so I must declare it.
Also, note that if you re-throw the exception like in the first example,
you should use "throw;" and not "throw ex;". See
Laurent Bugnion, GalaSoft
Software engineering: http://www.yqcomputer.com/
Support children in Calcutta: http://www.yqcomputer.com/