What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 10260 |  Welcome, Guest!   Register  Login
 Home > Interview Questions > C# Interview Questions > What is difference between throw and thr ...

What is difference between throw and throw ex?

Interview question and answer by: Rajni.Shekhar | Posted on: 6/27/2012 | Category: C# Interview questions | Views: 1043 | | Points: 40


Answer:

When you use throw ex to throw exception, it will update stack trace but in throw it will not update stack trace.

Example:
Class cls

{
public static void Main(string[] args)
{
try
{

function3();
Console.WriteLine("Catch Ex"); Console.ReadLine();

}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace); Console.ReadLine();
}
}

public static void function3()
{
try
{
function2();
}
catch (Exception ex)
{
throw;
}
}
public static void function2()
{
try
{
function1();
}
catch (Exception ex)
{
throw;
}
}
public static void function1()
{
try
{
int i = 0;
int j = 2 / i;
}
catch (DivideByZeroException ex)
{
throw;
}
}
}

Source: HCL | Asked In: Many Interviews | Alert Moderator 
Found interesting? Add this to:


>> Write Response - Respond to this post and get points

Even more ... | Submit Interview Questions and win prizes!

More Interview Questions from Rajni.Shekhar

Even more ... | Submit Interview Questions and win prizes!


About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2013 11:45:33 PM