Sunday, 19 January 2014

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

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

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;
}
}

No comments:

Post a Comment

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