473,386 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

exception statements

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
1 1641
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Frank Puck | last post by:
Meanwhile there are at least 8 years that compilers exist, which provide a working implementation of C++ Exception Handling. Has anything changed meanwhile? From my point of view nothing has...
6
by: Josh Mcfarlane | last post by:
I keep trying to get myself out of the return-code mindset, but it doesn't seem to work. They are suppose to get rid of if-then statements of return codes, but you still have to do an if statement...
0
by: Dmitry Tolpeko | last post by:
Hello, Could anybody please explain me why DB2 z/OS 8.1 fails on the simple procedure below: Error: DSNHSMS2 LINE 12 COL 6 NESTED COMPOUND STATEMENTS NOT ALLOWED It failes on DECLARE...
16
by: ChInKPoInt [No MCSD] | last post by:
I am using Visual Studio 2K3 writing a ASP.NET web application. Is there a way to force the C# compiler to catch possible exception? In Java, all exceptions thrown MUST BE caught, otherwise...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
15
by: Javier Estrada | last post by:
Can someone explaing the difference between these exception models regarding the structured exception handling? The documentation is not clear. Some code would actually help. Thx
2
by: Macca | last post by:
Hi, I have a switch statement that has 5+ case statements. Each of these case statements copies form one array to another. Rather than doing a separate try..catch statement for each case...
132
by: Zorro | last post by:
The simplicity of stack unraveling of C++ is not without defective consequences. The following article points to C++ examples showing the defects. An engineer aware of defects can avoid...
0
by: sethuraman | last post by:
Hi, I have had a problem before, I am not sure about the exact exception that is thrown.. but I am sure that there is an exception thrown after processing 600 and odd rows in a DB2 JDBC...
9
by: TC | last post by:
Hey All, I posted this to the Crypto users group and forgot to add the VB.Net users group. I apologize for any confusion. I have been testing a try / catch / finally block and purposely...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.