Wednesday, June 10, 2015

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

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.

10 comments:

  1. thanks for sharing this excellent information it's very useful to dot net learners.we are offering dot net online training if any body want to learn dot net plz visit the link....
    DOT NET ONLINE TRAINING

    ReplyDelete
  2. I think this is among the most important information for
    me. And i am glad reading your article.

    ReplyDelete
  3. First is to thank you for all this informative posts you give us for free; i bet all of us are happy.
    Such a great idea of yours! You have been a big help for me. Thanks a lot. more post for interesting topic. Great!

    SAP training in Chennai

    ReplyDelete
  4. Thanku for sharing this nice post..
    SAP HANA training in hyderabad,This is the best path You can Learn Complete Course with full fledge knowledge of SAP.

    ReplyDelete
  5. Great and useful article. Creating content regularly is very tough. Your points are motivated me to move on.


    SEO Company in Chennai

    ReplyDelete
  6. I have read your blog its very attractive and impressive. I like it your blog.

    Dot Net Training in Chennai Dot Net Training in Chennai .Net Online Training .Net Online Training Dot Net Training in Chennai Dot Net Training in Chennai


    Dot Net Online Training Dot Net Online Training LINQ Online Training LINQ Online Training ASP.NET Online Training ASP.NET Online Training

    ReplyDelete
  7. The blog gave me an idea about throw and throw ex. Thanks for sharing it
    DotNet Training in Chennai

    ReplyDelete
  8. Nice information about sap. i am looking SAP HANA Online Training with real time project.

    ReplyDelete

Followers