如何从嵌套Catch语句中转义

本文关键字:语句 转义 Catch 嵌套 | 更新日期: 2023-09-27 18:11:47

在下面的代码中,我有一个嵌套的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());
  }

如何从嵌套Catch语句中转义

您捕获了"内部"catch的异常,因此异常已被处理。如果您希望"外部"catch也触发,则必须重新抛出异常:

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