473,545 Members | 2,679 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Throwing exceptions

Hi,

Can anyone please tell me if there is any difference in throwing an
exception with or without parentheses, e.g.

try
{
/*
code
*/
}
catch (Exception ex)
{
throw (ex);
}

try
{
/*
code
*/
}
catch (Exception ex)
{
throw ex;
}
Any assistance gratefully received.

Mark
Nov 17 '05 #1
9 1503
No difference at all. But as a general rule you should not use the form..

1 try
2 {
3 if ( somethingWentWr ong)
4 throw new Exception();
5 }
6 catch(Exception ex)
7 {
8 throw ex;
9 }

because the stack trace is reset to the line of code where it is thrown, so
you lose the original stack trace. In other words, rather then saying an
exception occurred on line 4 it would report it occurred on line 8. You
would be better off simply using this...

catch(Exception ex)
{
throw;
}

or even better

catch(Exception ex)
{
throw new SomeExceptionTy pe("More information here.",ex);
}

The 2nd form is preferred because it preserves the original stack track and
exception type and allows you to add additional information.

The other form (throw;) will rethrow the original exception. It is
*supposed* to preserve the original stack trace but there is a bug in the
current runtime so that it acts the same as throw ex; in that it resets the
stack trace to begin on the line where the exception is rethrown.

"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:em******** ******@TK2MSFTN GP09.phx.gbl...
Hi,

Can anyone please tell me if there is any difference in throwing an
exception with or without parentheses, e.g.

try
{
/*
code
*/
}
catch (Exception ex)
{
throw (ex);
}

try
{
/*
code
*/
}
catch (Exception ex)
{
throw ex;
}
Any assistance gratefully received.

Mark

Nov 17 '05 #2
"David Levine" <no************ ******@wi.rr.co m> wrote in message
news:e3******** ******@TK2MSFTN GP14.phx.gbl...

David,

Thanks very much for the reply.
catch(Exception ex)
{
throw;
}
That, obviously, gives me a build warning that 'ex' is never used - not too
much of a problem, I suppose.
catch(Exception ex)
{
throw new SomeExceptionTy pe("More information here.",ex);
}


Fair enough. However, I don't actually need to throw a specific exception
here, because this is a base class (sorry, I should have mentioned that in
the OP). I simply want to throw the exception to the calling class, where I
will pass it to my generic exception handling routine, and then display a
friendly message to the user telling them that an error has occurred,
support has been notified, awfully sorry etc... In that case, would I be
better of simply using:

catch
{
throw;
}

Thanks,

Mark
Nov 17 '05 #3
Hi David,

The builder warning can be ignored, but to remove it remove (Exception ex)
from the catch

try
{
}
catch
{
}

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #4
Morten Wennevik wrote:
Hi David,

The builder warning can be ignored, but to remove it remove (Exception
ex) from the catch

try
{
}
catch
{
}


but then you might catch more than originally wanted
(if it used to be "catch(Specific Exception ex)")

Two other solutions:

catch (SpecificExcept ion)
{
throw;
}

or

catch (SpecificExcept ion ex)
{
ex = ex;
throw;
}

The first has no variable, so no warning.
The second "uses" the exception, so no warning is given,
and has the advantage during debugging that you can *see* what
exception is thrown (QuickView).

--
Hans Kesting
Nov 17 '05 #5
"Hans Kesting" <ne***********@ spamgourmet.com > wrote in message
news:OG******** ******@TK2MSFTN GP15.phx.gbl...

Hans,
but then you might catch more than originally wanted
(if it used to be "catch(Specific Exception ex)")

Two other solutions:

catch (SpecificExcept ion)
{
throw;
}

or

catch (SpecificExcept ion ex)
{
ex = ex;
throw;
}

The first has no variable, so no warning.
The second "uses" the exception, so no warning is given,
and has the advantage during debugging that you can *see* what
exception is thrown (QuickView).


Interesting.

In my particular case (a base class), I'm not particularly interested in
catching specific exceptions. If an exception occurs, I want the following
to happen:

1) The exception is caught (obviously!)

2) The exception is thrown to the method in the calling class.

3) The exception is passed to my generic exception handler class which,
among other things, sends me an email detailing the exception etc so that I
can investigate it.

4) Informs the user that whatever they were trying to do has been
unsuccessful, that Support has been informed etc...
Nov 17 '05 #6
Morten Wennevik wrote:
Hi David,

The builder warning can be ignored, but to remove it remove
(Exception ex) from the catch

try
{
}
catch
{
}


This catches non CLS compliant exceptions as well. To suppress the
warning, just don't specify a name for the Exception object:

try
{
//...
}
catch (Exception)
{
}

Cheers,

--
http://www.joergjooss.de
mailto:ne****** **@joergjooss.d e
Nov 17 '05 #7
"Joerg Jooss" <ne********@joe rgjooss.de> wrote in message
news:xn******** ********@msnews .microsoft.com. ..

So, in summary, is the consensus of opinion that, unless I want to test for
a specific exception, or throw a specific exception, the following code is
best to do nothing more than trap the most recent exception and throw it to
the calling method?

try
{
//...
}
catch (Exception)
{
throw;
}
Nov 17 '05 #8
Hi Dave,
I also found the problem the 2nd form (throw;) will reset the stack
trace,but sometime it is won't, if use below code, it will preserve the
original stack trace:
public static void F()
{
throw new Exception("Too lazy to implement!");
}

public static void Main()
{
try
{
F();
}
catch (Exception e)
{
Console.WriteLi ne("Exception {0} has occurred!", e.GetType());
throw;
}
}

Is there workaround for this problem without using the 3rd form?

Thanks

Si Jingnan

David Levine wrote:
No difference at all. But as a general rule you should not use the form..

1 try
2 {
3 if ( somethingWentWr ong)
4 throw new Exception();
5 }
6 catch(Exception ex)
7 {
8 throw ex;
9 }

because the stack trace is reset to the line of code where it is thrown, so
you lose the original stack trace. In other words, rather then saying an
exception occurred on line 4 it would report it occurred on line 8. You
would be better off simply using this...

catch(Exception ex)
{
throw;
}

or even better

catch(Exception ex)
{
throw new SomeExceptionTy pe("More information here.",ex);
}

The 2nd form is preferred because it preserves the original stack track and
exception type and allows you to add additional information.

The other form (throw;) will rethrow the original exception. It is
*supposed* to preserve the original stack trace but there is a bug in the
current runtime so that it acts the same as throw ex; in that it resets the
stack trace to begin on the line where the exception is rethrown.

"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:em******** ******@TK2MSFTN GP09.phx.gbl...
Hi,

Can anyone please tell me if there is any difference in throwing an
exception with or without parentheses, e.g.

try
{
/*
code
*/
}
catch (Exception ex)
{
throw (ex);
}

try
{
/*
code
*/
}
catch (Exception ex)
{
throw ex;
}
Any assistance gratefully received.

Mark


Nov 17 '05 #9

"Si Jingnan" <ji********@gma il.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
Hi Dave,
I also found the problem the 2nd form (throw;) will reset the stack
trace,but sometime it is won't, if use below code, it will preserve the
original stack trace:
public static void F()
{
throw new Exception("Too lazy to implement!");
}

public static void Main()
{
try
{
F();
}
catch (Exception e)
{
Console.WriteLi ne("Exception {0} has occurred!", e.GetType());
throw;
}
}

Is there workaround for this problem without using the 3rd form?


The test I ran got different results then you...it always set the stack
trace to the throw statement, so there is no workaround that I know of.

Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
4665
by: Mark Oueis | last post by:
I've been struggling with this question for a while. What is better design? To design functions to return error codes when an error occures, or to have them throw exceptions. If you chose the former, i have a few questions that need to be answered. 1) What about functions that need to return a value regardless of the error. How can they...
21
4393
by: mihai | last post by:
People say that is a bad technique to throw exception from constructors; and that the solution would be to create a function _create_ to initialize an object. What about copy constructors? How can we avoid throwing exceptions? If we already have an abject witch was initialized wit _create_ we will be forced to call create in copy...
40
13473
by: Kevin Yu | last post by:
is it a bad programming design to throw exception in the try block then catch it??
5
282
by: Mark Oueis | last post by:
I've been struggling with this question for a while. What is better design? To design functions to return error codes when an error occures, or to have them throw exceptions. If you chose the former, i have a few questions that need to be answered. 1) What about functions that need to return a value regardless of the error. How can they...
40
36068
by: Sek | last post by:
Is it appropriate to throw exception from a constructor? Thats the only way i could think of to denote the failure of constructor, wherein i am invoking couple of other classes to initialise the object. TIA Sek
0
7496
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7685
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7452
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6014
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5354
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3485
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1916
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
738
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.