ASP.NET - Pass Exception occured in child method to the parent method

Asked By aman on 23-Jan-12 05:53 AM
hello friends,

i want to pass the exception occurred in the child method to the exception of the parent method 

for example: - 
try
{
  try
  {
//error occured
}
catch()
{
}
}
catch()
{
 // the error occured above i want to pass here and display 
}


please help me ........

please also let me known if i have not made my question clear.

thanks,
kalpana aparnathi replied to aman on 23-Jan-12 06:15 AM
Use this code for solution:

   
void Application_Error(object sender, EventArgs e)
{
    // Code that runs when an un-handled error occurs
    Exception ex = Server.GetLastError();
    Logger.LogException(ex);
}
Riley K replied to aman on 23-Jan-12 06:31 AM


Do you really need to throw it again ??

as a workaround you can try something like this 


bool ok = true;
 
      try
      {
        //SECTION A
 
        try
        {
          //SECTION B
        }
        catch
        {
          ok = false;throw;
        }
 
        if (ok)
        {
          //SECTION C
        }
 
      }
      catch
      {
        if (ok)
        {
          throw new Exception("Error occured in Section A or C.");
        }
        else
        {
          throw new Exception("Error occured in Section B.");
        }
      }

Regards
dipa ahuja replied to aman on 23-Jan-12 06:41 AM
Better to use the single try catch for a block. Just write the code which you want to check for exception in the try and in catch which catches and handle the error