DotNetSlackers: ASP.NET News for lazy Developers

Thursday, October 15, 2015

Difference between throw and throw ex in Asp.Net C# Exception

Exceptions

Exceptions are always useful for dotnet developers, because we are able to provide a bug free application with the use of exception handlings.We need to find the actual exception, here throw will give the exception details.We are confusing whileusing the throw and throw ex in exceptions.Difference between "throw" and "throw ex" is, "throw" will provide the orginal exception which was available at first in stack and "throw ex" will provide the last exception message available in stack.

Syntax for "throw ex"



try
{
   //If error occured go to catch and throw exception
}
catch (Exception ex)
{
   throw ex;
}

Syntax for "throw"


try
{
   //If error occured go to catch and throw exception
}
catch
{
   throw;
}
Eg: While working in N layered architecture application, "throw ex" will be use in UI Layer and "throw" will be use in all other layers.So try this in your application and check the difference.

Output
In this post i tried to explain the difference between throw and throw ex in Asp.Net C# Exceptions. My previous post Difference between dataset and datareader in Asp.Net C# exceptions.

No comments:

Post a Comment