如何从嵌套的Catch语句中逃脱

时间:2016-10-06 16:03:34

标签: c# try-catch

在下面的代码中,我有一个嵌套的Try Catch。在我的嵌套catch失败的情况下,我只想到父catch语句并执行该代码。我怎么能这样做?

try
  {
    try
    {   // build and send invoice lines
      ReadInvLinesToArray(row["ID_INVOICE"].ToString());
    }
    catch (Exception e)
    {
      writeToEventLog(e.ToString(), true, false);
      SendErrorEmail("Failed to send Invoice Lines to NAV.  The following system error was generated: \n" + e.ToString());
    }
    // send invoice header if lines have been sent
    bool result = navInvoices.SendInvoicesToNAV(navImportInvoices);
    // update the retrieved records, marking QB Status as value N, passing in the sql dataset as a list        
    UpdateQBStatusInvoiceSent(ref idInvoicesSent);
  }
catch (Exception e)
  {
  // remove Invoice from list to ensure its status is not updated.
  idInvoicesSent.Remove(Convert.ToInt32(row["ID_INVOICE"]));

  WriteToEventLog(e.ToString(), true, false);
  SendErrorEmail("Failed to send Invoices to NAV.  The following system error was generated: \n" + e.ToString());
  }

1 个答案:

答案 0 :(得分:3)

您使用“内部”catch解决了异常,因此异常处理了它。如果你想要触发“外部”catch,那么你必须重新审视异常:

try {
   try {
     ...
   } catch (Exception e) {
      ... do stuff
      throw e; // you need this
   }
} catch (Exception e) {
   ... catch the re-thrown "e"
}