簡體   English   中英

為什么不嘗試{} catch {}在C#中工作?

[英]Why doesn't try {} catch{} work in C#?

我在Visual Studio的程序中有一個try{} catch{}塊。 try{}塊中,我具有連接到DB的代碼,當我運行該程序時,它將返回錯誤。 在這種情況下,程序必須執行catch{}塊,但是try{}塊將返回錯誤,程序將停止。

try {
  using (OracleConnection cn = new OracleConnection(DataBase.EDS_SDE_Connection)) {
    cn.Open();
    using (OracleCommand cm = cn.CreateCommand()) {
      cm.CommandType = CommandType.StoredProcedure;
      cm.CommandText = "EDS_FEAUTRE_CLASSES.DIS_ENA_TRG_LOG";
      OracleParameter oParam = new OracleParameter("PISDISABLE", OracleDbType.Decimal);
      oParam.Direction = ParameterDirection.Input;
      oParam.Value = pIsDisable;
      cm.Parameters.Add(oParam);
      cm.ExecuteNonQuery();
    }
  }
  bRet = true;
} catch (Exception ex) {
  ClsShares.ShowMessage("", ex.Message);
  bRet = false;
}

那是從下面突出顯示的chekbox發出的:

在此處輸入圖片說明

默認情況下,並非所有異常類型都啟用來中斷解決方案。 您可以從“異常設置”窗口中選擇啟用的選項,如上圖所示。 設置要拋出的CLR異常。 這就是代碼將如何中斷(如果沒有try / catch的話),或者在您的情況下,try序列將中斷,並且catch塊將被執行。

對於不同的IDE:

VS2015: Debug -> Windows -> Exception Settings.

VS2012: Debug -> Exceptions more details

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM