Answer
Mehmood
Online
5/17/2012 11:07:51 PM
Recommended approach is to throw with an exception object so that it could be re thrown and more customized.
throw ex resets the stack trace (so your errors would appear to originate from HandleException) throw doesn't - the original offender would be preserved.
You can see a nice code example by navigating below path:
http://www.codeproject.com/Questions/294287/Difference-between-throw-and-throw-ex-in-Csharp
|
Answer
Iram
Online
5/28/2012 12:24:32 PM
It means that the stack trace method is the main difference between these two methods. As the empty parameter re-throw keeps the existing stack list, whereas, the parametrized version creates a new stack trace to the point of the throw.
|
Answer
Mehmood
Online
5/28/2012 2:13:21 PM
Hi..
Let me know and sorry please if i was and still now unable to deliver the actual meaning.
An exception carries the stack trace information which is very useful.
Often, we need to put some exception handling on catch blocks
(e.g., to rollback a transaction or generate a log in form of text file or in EventViewer) and re-throw the exception.
See the right (and the wrong) way of doing it:
The wrong way:
try { // Some code that throws an exception } catch (Exception ex) { // some code that handles the exception throw ex; }
Why is this wrong?
Because, when you examine the stack trace, the point of the exception will be the line of the "throw ex;", hiding the real error location.
Correct one is:
try { // Some code that throws an exception } catch (Exception ex) { // some code that handles the exception throw; }
What has changed?
Instead of "throw ex;", which will throw a new exception and clear the stack trace, we have simply "throw;".
If you don't specify the exception, the throw statement will simply rethrow the very same exception the catch statement caught.
This will keep your stack trace intact, but still allows you to put code in your catch blocks.
|
Answer
Mehmood
Online
5/29/2012 7:00:52 PM
Hi .
I am sorry, my submitted observation was wrong.
I tried my ownself but couldn't find any difference.
If you try to debug below code, your 'll see the same Stack Trace (either throw or throw with ex object).
Right click on the ex1 and choose quickwatch, now scroll down and you 'll see BinaryStackTraceArray property.
Now note the array in each below case with both ex1 and ex2.
i have both snapshots but couldn't upload (due to the restriction policy at my machine right now) nor even CodingVilla have this facility.
====================================throw with exception
System.Exception ex1, ex2;
try { try { try { int firstNumber = 2, secondNumber = 0; decimal sum = 0; sum = firstNumber / secondNumber; } catch (System.Exception ex) { ex1 = ex; throw ex1; }
} catch (System.Exception ex) { ex2 = ex; throw ex2; }
} catch (System.Exception) { throw; }
======================================throw without exception
System.Exception ex1, ex2;
try { try { try { int firstNumber = 2, secondNumber = 0; decimal sum = 0; sum = firstNumber / secondNumber; } catch (System.Exception ex) { ex1 = ex; throw; }
} catch (System.Exception ex) { ex2 = ex; throw; }
} catch (System.Exception) { throw; }
|