Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

C# - Difference between Throw and Throw ex in C# Asp.Net

Nov 28, 2013
Introduction

Here I will explain what is the difference between throw and throw ex in c# using asp.net.

Description


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.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

8 comments :

Anonymous said...

Good one. thanks ....

Jayanti said...

nice article. please add example then it will be more beneficial to all.

Anonymous said...

nive explanation...

Unknown said...

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..

Unknown said...

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

kinjalshah said...

Very nice Article....thanks

Unknown said...
This comment has been removed by the author.
Unknown said...

Not clear explanation

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.