Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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

Tags: throw
Introduction

In this post I will try 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.

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.


This post first appeared on Dotnet, please read the originial post: here

Share the post

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

×

Subscribe to Dotnet

Get updates delivered right to your inbox!

Thank you for your subscription

×