Introduction
Description
In previous articles I explained Different types of polymorphism in c#, Difference between array and arraylist in c#, can function return multiple values in sql server, Difference between char varchar and nvarchar in SQL Server,
Joins in SQL Server and many articles relating to interview questions, SQL
Server. Now I will explain difference between throw and throw ex in c#
using asp.net.
Throw
In Throw, the original exception stack trace will be
retained. To keep the original stack trace information, the correct syntax is
'throw' without specifying an exception.
Declaration of throw
try
{
// do some operation that can fail
}
catch (Exception ex)
{
// do some local cleanup
throw;
}
|
Throw ex
In Throw ex, the original stack trace information will
get override and you will lose the original exception stack trace. I.e. 'throw
ex' resets the stack trace.
Declaration of throw ex
try
{
// do some operation that can fail
}
catch (Exception ex)
{
// do some local cleanup
throw ex;
}
}
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
8 comments :
Good one. thanks ....
nice article. please add example then it will be more beneficial to all.
nive explanation...
should i answer this for an interview.If this question is asked.If i do the interviewer will feel as if i am in std 5-6-7-8 by hearting answer.what does ur sentence mean.Give a clear explanation stack trace ?? over ride ?? ..these sentences are available online anywhere..and there are people who support for nice explanations..
using System;
static class Program
{
static void Main()
{
try
{
ThrowTest();
}
catch (Exception e)
{
Console.WriteLine("Your stack trace:");
Console.WriteLine(e.StackTrace);
Console.WriteLine();
if (e.InnerException == null)
{
Console.WriteLine("No inner exception.");
}
else
{
Console.WriteLine("Stack trace of your inner exception:");
Console.WriteLine(e.InnerException.StackTrace);
}
}
}
static void ThrowTest()
{
decimal a = 1m;
decimal b = 0m;
try
{
Mult(a, b); // line 34
Div(a, b); // line 35
Mult(b, a); // line 36
Div(b, a); // line 37
}
catch (ArithmeticException arithExc)
{
Console.WriteLine("Handling a {0}.", arithExc.GetType().Name);
// uncomment EITHER
//throw arithExc;
// OR
//throw;
// OR
//throw new Exception("We handled and wrapped your exception", arithExc);
}
}
static void Mult(decimal x, decimal y)
{
decimal.Multiply(x, y);
}
static void Div(decimal x, decimal y)
{
decimal.Divide(x, y);
}
}
If you uncomment the throw arithExc; line, your output is:
Handling a DivideByZeroException.
Your stack trace:
at Program.ThrowTest() in c:\somepath\Program.cs:line 44
at Program.Main() in c:\somepath\Program.cs:line 9
No inner exception.
Certainly, you have lost information about where that exception happened. If instead you use the throw; line, this is what you get:
Handling a DivideByZeroException.
Your stack trace:
at System.Decimal.FCallDivide(Decimal& d1, Decimal& d2)
at System.Decimal.Divide(Decimal d1, Decimal d2)
at Program.Div(Decimal x, Decimal y) in c:\somepath\Program.cs:line 58
at Program.ThrowTest() in c:\somepath\Program.cs:line 46
at Program.Main() in c:\somepath\Program.cs:line 9
No inner exception
Very nice Article....thanks
Not clear explanation
Note: Only a member of this blog may post a comment.