为什么不尝试{} catch {}在C#中工作? [英] Why doesn't try {} catch{} work in C#?

查看:80
本文介绍了为什么不尝试{} catch {}在C#中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Visual Studio中的程序中有一个 try {} catch {} 块。在 try {} 块中,我有连接到数据库的代码,当我运行该程序时,它将返回错误。在这种情况下,程序必须执行 catch {} 块,但是 try {} 块返回错误

I have a try{} catch{} block in my program in Visual Studio. In the try{} block I have code to connect to a DB and when I run that program it returns an error. In this situation the program must execute the catch{} block, but an error is returned from the try{} block and the program stops.

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:

That's was from bellow highlighted chekbox:

推荐答案

并非所有异常类型都可以启用默认情况下破坏解决方案。您可以从异常设置窗口中选择启用的选项,如上图所示。
设置要抛出的CLR异常。这就是代码将如何中断(如果没有try / catch的话),或者在您的情况下,try序列将中断,并且catch块将被执行。

Not all exception types are enabled to break the solution by default. You can choose which are enabled from the Exception Setting window as you show above in the image. Set the CLR exceptions set to be thrown. That's how the code will break (if there is no a try/catch) or in your case the try sequence will break and the catch block will be executed.

IDE:

VS2015: Debug -> Windows -> Exception Settings.

VS2012: Debug -> Exceptions more details

这篇关于为什么不尝试{} catch {}在C#中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆