Catch块没有捕获异常为什么? [英] Catch block does not catch the exception Why?

查看:84
本文介绍了Catch块没有捕获异常为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码是....

my code is....

public static bool SaveChitGroupDetails(ChitGroup obj)
  {
  try
  {
   _dal = DataLayer.GetInstance();
   _dal.Sql = StoredProcedures.SaveChitGroupDetails;
  _dal.CommandType = CommandType.StoredProcedure;
  _dal.AddParameter("_grcode",obj.GrpCode, DbType.String,ParameterDirection.Input);
              _dal.AddParameter("_brid", obj.Brid, DbType.Int16, ParameterDirection.Input);
              _dal.AddParameter("_prdcode", obj.Prdcode, DbType.String, ParameterDirection.Input);
              _dal.AddParameter("_valuecode", obj.Valuecode, DbType.String, ParameterDirection.Input);
              _dal.AddParameter("_companychit", obj.Companychit, DbType.Boolean, ParameterDirection.Input);
              _dal.AddParameter("_series", obj.Series, DbType.String, ParameterDirection.Input);
              _dal.AddParameter("_status", obj.Status, DbType.String, ParameterDirection.Input);
              _dal.AddParameter("_bylawnumber", obj.Bylawnumber, DbType.String, ParameterDirection.Input);
              _dal.AddParameter("_registrationdate", obj.RegistrationDate, DbType.Date, ParameterDirection.Input);
              _dal.AddParameter("_commencedate", obj.CommenceDate, DbType.Date, ParameterDirection.Input);
              _dal.AddParameter("_terminatedate", obj.TerminateDate, DbType.Date, ParameterDirection.Input);
              _dal.AddParameter("_maxdiscount", obj.MaxDiscount, DbType.Int16, ParameterDirection.Input);
              _dal.AddParameter("_active", obj.Active, DbType.Boolean, ParameterDirection.Input);
              _dal.AddParameter("_userid",obj.Userid,DbType.String,ParameterDirection.Input);
              _dal.AddParameter("_amndmntdate", obj.AmendmentDate, DbType.Date, ParameterDirection.Input);
              _dal.AddParameter("_amndtrmntdate", obj.AmendTerminateDate, DbType.Date, ParameterDirection.Input);
              
int rtrnid = _dal.ExecuteNonQuery();
              return true;
          }
          
catch (Exception ex)
          {
              throw ex;
              return false;
          }
      
    finally
          {
              if (_dal.Connected)
                  _dal.Connection.Close();
              //return true;
          }
      }

推荐答案

该代码不起作用:它(正确)发出警告,忽略:

That code won't work: it (correctly) gives a warning that you are ignoring:
Unreachable code detected



这是因为你的方法永远不会返回 false - 抛出异常将控制转移到最近的catch块,而无需直接返回调用方法。



在您的方法中放置一个断点,并确切地遵循发生的情况:当您的数据库活动失败时,执行将移动到catch块,然后保留该方法而不返回到它的调用位置。如果发生错误,它将不会返回true。



错别字[/ edit]


This is because your method can never return false - the throw of the exception transfers control to the "closest" catch block immediately, without needing to return to the calling method directly.

Put a breakpoint in your method, and follow exactly what happens: when your database activity fails, execution will move to the catch block, and then leave the method without ever returning to where it was called from. It will not return true if an error occurs.

[edit]Typos[/edit]


这篇关于Catch块没有捕获异常为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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