1

以下に示すコード(簡略化)のコンソールアプリケーションがあります。

public static void Main(string[] args)
{
  AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper;

  InsertRow();
}

private static void InsertRow();
{
  DAO.DBEngine dbEngine = new DAO.DBEngine();
  DAO.Database db = dbEngine.OpenDatabase("database_name");
  DAO.Recordset rs = db.OpenRecordset("table_name");
  DAO.Field myField = new DAO.Field();
  rs.AddNew();
  // System.Runtime.InteropServices.COMException occurs here, but only in the IDE Debugger:
  myField.Value = "oops, this value is bigger than the size of the column";
  rs.Close();
  db.Close();
}

private static void UnhandledExceptionTrapper(object sender, UnhandledExceptionEventArgs e)
{
  // Log error
  Environment.Exit(1);
}

COMExceptionがUnhandledExceptionイベントをトリガーしないのはなぜですか?

私の実際のコードでは、DAOのものは別のクラスにあります。私catchはCOMExceptionを試し、以下のようにもう一度スローしました。

private static void InsertRow();
{
  try
  {
    ... // See code above
    myField.Value = "oops, this value is bigger than the size of the column";
  }
  catch (System.Runtime.InteropServices.COMException ex)
  {
    throw new Exception(ex.Message, ex.InnerException);
  }
  finally
  {
    rs.Close();
    db.Close();
  }
}

しかし、それもうまくいきませんでした。 何がわからないの?

ところで、これは機能しますが、私はこのようにしたくありませんでした:

  catch (System.Runtime.InteropServices.COMException ex)
  {
    // Log error
    Environment.Exit(1);
  }

タンブルウィードを始めましょう!

4

0 に答える 0