exception statements

C

cody

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 ****ing ads]
 
J

Jon Skeet

cody said:
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.
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.
 

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