What is the use of throw statement?

 Posted by Bharathi Cherukuri on 2/2/2012 | Category: ASP.NET Interview questions | Views: 3546 | Points: 40
Answer:

A throw statement is used to generate exception explicitly.
This throw statement is generally used in recording error in event log or sending an Email notification about the error.
Avoid using throw statement as it degrades the speed.

Example:

try

{
//Code that might generate error
}
catch(Exception error)
{
//Code that handle errors occurred in try block

throw; //Re throw of exception to add exception details in event log or sending email
}

//Code that handle errors occurred in try block
}
finally
{
//Code to dispose all allocated resources
}

Format2

try
{
//Code that might generate error
}
finally
{
//Code to dispose all allocated resources
}


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response