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 9 1494
No difference at all. But as a general rule you should not use the form..
1 try
2 {
3 if ( somethingWentWrong)
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 SomeExceptionType("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**************@TK2MSFTNGP09.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
"David Levine" <no******************@wi.rr.com> wrote in message
news:e3**************@TK2MSFTNGP14.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 SomeExceptionType("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
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]
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(SpecificException ex)")
Two other solutions:
catch (SpecificException)
{
throw;
}
or
catch (SpecificException 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
"Hans Kesting" <ne***********@spamgourmet.com> wrote in message
news:OG**************@TK2MSFTNGP15.phx.gbl...
Hans, but then you might catch more than originally wanted (if it used to be "catch(SpecificException ex)")
Two other solutions:
catch (SpecificException) { throw; }
or
catch (SpecificException 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...
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.de
"Joerg Jooss" <ne********@joergjooss.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;
}
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.WriteLine("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 ( somethingWentWrong) 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 SomeExceptionType("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**************@TK2MSFTNGP09.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
"Si Jingnan" <ji********@gmail.com> wrote in message
news:%2****************@TK2MSFTNGP09.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.WriteLine("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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
by: Kevin Yu |
last post by:
is it a bad programming design to throw exception in the try block then
catch it??
|
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...
|
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...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
| |