By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,885 Members | 1,474 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,885 IT Pros & Developers. It's quick & easy.

exception statements

P: n/a
is there any difference between

try{}catch(Exception){}

and

try{}catch{} ?

if not, why this short syntax? i think it encourages programmers especially
beginners to catch all exceptions instead of catching a specific exception
which is a better style of programming. one could only under rare
circumstances catch all exceptions.

and i have another question. is there a difference between:

catch(Exception e){throw e;}

and

catch(Exception e){throw new Exception(e);}

which one is better for rethrowing an exception?

thx for answering me in advance!

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu
[noncommercial and no fucking ads]

Nov 15 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
cody <de********@gmx.net> wrote:
is there any difference between

try{}catch(Exception){}

and

try{}catch{} ?
Yes. There's a note in the C# language spec:

<quote>
Note: Some environments, especially those supporting multiple
languages, may support exceptions that are not representable as an
object derived from System.Exception, although such an exception could
never be generated by C# code. In such an environment, a general catch
clause might be used to catch such an exception. Thus, a general catch
clause is semantically different from one that specifies the type
System.Exception, in that the former may also catch exceptions from
other languages.
</quote>
and i have another question. is there a difference between:

catch(Exception e){throw e;}

and

catch(Exception e){throw new Exception(e);}
The latter creates a new exception which wraps the old exception up in
it. The former rethrows the old exception.
which one is better for rethrowing an exception?


It depends on what you want to do. Often you want to throw a new
exception of a different type, giving the old exception as a more
detailed reason.

There's a third way, however:

throw;

which rethrows the original exception without changing its stack trace.
"throw e;" will start the stack trace again, but I find I usually want
to see the trace as far as it can possibly go.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.