Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未处理的运行时错误c# private void SetConnection(字符串id、字符串类代码) { 尝试 { _connection=new SqlConnection{ConnectionString=Settings.Default.CurrentConnection}; _connection.Open(); while(_connection.State==ConnectionState.Connecting | | | u connection.State==ConnectionState.Closed) 睡眠(1000); _command=newsqlcommand(Settings.Default.EligibilityBenefitSP,_连接); 如果(_command!=null)_command.CommandType=CommandType.storedProcess; _command.Parameters.Add(“@ClassCode”,SqlDbType.NVarChar).Value=ClassCode; _Add(“@Id”,SqlDbType.NVarChar).Value=Id; } 捕获(例外e) { 抛出新异常(e.Message+“”+Settings.Default.EligibilityBenefitSP); } } 公共集合ExtractEligibilityClassBenefit(字符串id、字符串类代码) { 设置连接(id、类代码); Collection eclassBene=新集合(); SqlDataReader=null; 尝试 { _command.CommandTimeout=420; if(_connection.State==ConnectionState.Open) reader=\u command.ExecuteReader(CommandBehavior.CloseConnection); 其他的 抛出新异常(“连接已关闭”); /*没有数据*/ 如果(!reader.HasRows)返回null; while(reader.Read()) { EligibilityClassBenefit EligibilityClassBenefit=新的EligibilityClassBenefit { EffectiveDate=读取器[“EffectiveDate”]。ToString(), EndDate=读卡器[“EndDate”]。ToString(), InitialEffectiveDate=读取器[“InitialEffectiveDate”]。ToString(), IsAdministrativeServicesOnly=读卡器[“IsAdministrativeServicesOnly”]。ToString(), EffectiveProvision=读卡器[“EffectiveProvision”]。ToString(), 试用期=读取器[“试用期”]。ToString(), UnderwritingType=提取UnderwritingType(id), PermissionPeriodUnit=读卡器[“PermissionPeriodUnit”]。ToString(), StateOfIssue=读卡器[“StateOfIssue”]。ToString(), }; BenefitData BenefitData=新的BenefitData(); eligibilityClassBenefit.Benefit=benefitData.ExtractBenefit(读卡器、id、类代码); EligibilityClassBenefitBusinessLevelData eligibilityLevelData=新的EligibilityClassBenefitBusinessLevelData(); eligibilityClassBenefit.EligibilityClassBenefitBusinessLevelNodes=eligibilityLevelData.ExtractBenefitBusinessLevel(读卡器); eclassBene.Add(EligibilityClassBene); } 返回eclassBene; } 捕获(例外e) { 抛出新异常(e.InnerException.Message+e.InnerException.StackTrace); } 最后 { //如果(_connection.State==ConnectionState.Open)_connection.Close(); if(reader!=null)reader.Close(); _command.Dispose(); } }_C#_Sql Server_Runtime - Fatal编程技术网

未处理的运行时错误c# private void SetConnection(字符串id、字符串类代码) { 尝试 { _connection=new SqlConnection{ConnectionString=Settings.Default.CurrentConnection}; _connection.Open(); while(_connection.State==ConnectionState.Connecting | | | u connection.State==ConnectionState.Closed) 睡眠(1000); _command=newsqlcommand(Settings.Default.EligibilityBenefitSP,_连接); 如果(_command!=null)_command.CommandType=CommandType.storedProcess; _command.Parameters.Add(“@ClassCode”,SqlDbType.NVarChar).Value=ClassCode; _Add(“@Id”,SqlDbType.NVarChar).Value=Id; } 捕获(例外e) { 抛出新异常(e.Message+“”+Settings.Default.EligibilityBenefitSP); } } 公共集合ExtractEligibilityClassBenefit(字符串id、字符串类代码) { 设置连接(id、类代码); Collection eclassBene=新集合(); SqlDataReader=null; 尝试 { _command.CommandTimeout=420; if(_connection.State==ConnectionState.Open) reader=\u command.ExecuteReader(CommandBehavior.CloseConnection); 其他的 抛出新异常(“连接已关闭”); /*没有数据*/ 如果(!reader.HasRows)返回null; while(reader.Read()) { EligibilityClassBenefit EligibilityClassBenefit=新的EligibilityClassBenefit { EffectiveDate=读取器[“EffectiveDate”]。ToString(), EndDate=读卡器[“EndDate”]。ToString(), InitialEffectiveDate=读取器[“InitialEffectiveDate”]。ToString(), IsAdministrativeServicesOnly=读卡器[“IsAdministrativeServicesOnly”]。ToString(), EffectiveProvision=读卡器[“EffectiveProvision”]。ToString(), 试用期=读取器[“试用期”]。ToString(), UnderwritingType=提取UnderwritingType(id), PermissionPeriodUnit=读卡器[“PermissionPeriodUnit”]。ToString(), StateOfIssue=读卡器[“StateOfIssue”]。ToString(), }; BenefitData BenefitData=新的BenefitData(); eligibilityClassBenefit.Benefit=benefitData.ExtractBenefit(读卡器、id、类代码); EligibilityClassBenefitBusinessLevelData eligibilityLevelData=新的EligibilityClassBenefitBusinessLevelData(); eligibilityClassBenefit.EligibilityClassBenefitBusinessLevelNodes=eligibilityLevelData.ExtractBenefitBusinessLevel(读卡器); eclassBene.Add(EligibilityClassBene); } 返回eclassBene; } 捕获(例外e) { 抛出新异常(e.InnerException.Message+e.InnerException.StackTrace); } 最后 { //如果(_connection.State==ConnectionState.Open)_connection.Close(); if(reader!=null)reader.Close(); _command.Dispose(); } }

未处理的运行时错误c# private void SetConnection(字符串id、字符串类代码) { 尝试 { _connection=new SqlConnection{ConnectionString=Settings.Default.CurrentConnection}; _connection.Open(); while(_connection.State==ConnectionState.Connecting | | | u connection.State==ConnectionState.Closed) 睡眠(1000); _command=newsqlcommand(Settings.Default.EligibilityBenefitSP,_连接); 如果(_command!=null)_command.CommandType=CommandType.storedProcess; _command.Parameters.Add(“@ClassCode”,SqlDbType.NVarChar).Value=ClassCode; _Add(“@Id”,SqlDbType.NVarChar).Value=Id; } 捕获(例外e) { 抛出新异常(e.Message+“”+Settings.Default.EligibilityBenefitSP); } } 公共集合ExtractEligibilityClassBenefit(字符串id、字符串类代码) { 设置连接(id、类代码); Collection eclassBene=新集合(); SqlDataReader=null; 尝试 { _command.CommandTimeout=420; if(_connection.State==ConnectionState.Open) reader=\u command.ExecuteReader(CommandBehavior.CloseConnection); 其他的 抛出新异常(“连接已关闭”); /*没有数据*/ 如果(!reader.HasRows)返回null; while(reader.Read()) { EligibilityClassBenefit EligibilityClassBenefit=新的EligibilityClassBenefit { EffectiveDate=读取器[“EffectiveDate”]。ToString(), EndDate=读卡器[“EndDate”]。ToString(), InitialEffectiveDate=读取器[“InitialEffectiveDate”]。ToString(), IsAdministrativeServicesOnly=读卡器[“IsAdministrativeServicesOnly”]。ToString(), EffectiveProvision=读卡器[“EffectiveProvision”]。ToString(), 试用期=读取器[“试用期”]。ToString(), UnderwritingType=提取UnderwritingType(id), PermissionPeriodUnit=读卡器[“PermissionPeriodUnit”]。ToString(), StateOfIssue=读卡器[“StateOfIssue”]。ToString(), }; BenefitData BenefitData=新的BenefitData(); eligibilityClassBenefit.Benefit=benefitData.ExtractBenefit(读卡器、id、类代码); EligibilityClassBenefitBusinessLevelData eligibilityLevelData=新的EligibilityClassBenefitBusinessLevelData(); eligibilityClassBenefit.EligibilityClassBenefitBusinessLevelNodes=eligibilityLevelData.ExtractBenefitBusinessLevel(读卡器); eclassBene.Add(EligibilityClassBene); } 返回eclassBene; } 捕获(例外e) { 抛出新异常(e.InnerException.Message+e.InnerException.StackTrace); } 最后 { //如果(_connection.State==ConnectionState.Open)_connection.Close(); if(reader!=null)reader.Close(); _command.Dispose(); } },c#,sql-server,runtime,C#,Sql Server,Runtime,上面是一个代码示例,其中包含一个常规异常捕获,但当我运行此程序时,它将随机中断,并将未处理的异常与.net运行时错误null引用异常一起发送到应用程序日志 一点背景…这是一个控制台应用程序,在午夜自动运行在应用服务器上。它针对不同的SQLServer2008框执行存储过程。在执行维护任务时,当sql server断开连接时,我们常常会遇到这些错误,但情况已不再如此。我需要得到一个mmore特定的错误。我不明白为什么它会绕过catch子句而只是抛出一个未处理的运行时异常。这是什么意思?它发生在任意

上面是一个代码示例,其中包含一个常规异常捕获,但当我运行此程序时,它将随机中断,并将未处理的异常与.net运行时错误null引用异常一起发送到应用程序日志


一点背景…这是一个控制台应用程序,在午夜自动运行在应用服务器上。它针对不同的SQLServer2008框执行存储过程。在执行维护任务时,当sql server断开连接时,我们常常会遇到这些错误,但情况已不再如此。我需要得到一个mmore特定的错误。我不明白为什么它会绕过catch子句而只是抛出一个未处理的运行时异常。这是什么意思?它发生在任意数量的代码点上,而不仅仅是这一点。这只是上一个例子,当您捕获异常时,最后一个异常被破坏了,您也将它们抛出,由调用方处理。现在,您发布的代码中没有入口点,因此很难看到此代码段之外的内容

然而,粗略猜测一下,我对NullRef异常的起源的建议是这样做的:
e.InnerException.Message

InnerException
属性很可能为null,这将导致NullRef异常。然而,这并不是真正的例外。由于上述错误,导致程序最终进入异常处理程序的真正异常被隐藏

如果要包含来自InnerException的消息,请首先检查它是否为null

编辑:

这样做:

 private void SetConnection(string id, string classCode)
    {
        try
        {
            _connection = new SqlConnection { ConnectionString = Settings.Default.CurrentConnection };
            _connection.Open();
            while (_connection.State == ConnectionState.Connecting || _connection.State == ConnectionState.Closed)
                Thread.Sleep(1000);

            _command = new SqlCommand(Settings.Default.EligibilityBenefitSP, _connection);
            if (_command != null) _command.CommandType = CommandType.StoredProcedure;
            _command.Parameters.Add("@ClassCode", SqlDbType.NVarChar).Value = classCode;
            _command.Parameters.Add("@Id", SqlDbType.NVarChar).Value = id;
        }
        catch (Exception e)
        {
            throw new Exception(e.Message + " " + Settings.Default.EligibilityBenefitSP);
        }

    }

   public Collection<EligibilityClassBenefit> ExtractEligibilityClassBenefit(string id, string classCode)
    {
        SetConnection(id, classCode);
        Collection<EligibilityClassBenefit> eclassBene = new Collection<EligibilityClassBenefit>();
        SqlDataReader reader = null;
        try
        {
            _command.CommandTimeout = 420;
            if (_connection.State == ConnectionState.Open)
                reader = _command.ExecuteReader(CommandBehavior.CloseConnection);
            else
                throw new Exception("Connection Closed");

                /* no data */
                if (!reader.HasRows) return null;

                while (reader.Read())
                {
                    EligibilityClassBenefit eligibilityClassBenefit = new EligibilityClassBenefit
                    {
                        EffectiveDate                = reader["EffectiveDate"].ToString(),
                        EndDate                      = reader["EndDate"].ToString(),
                        InitialEffectiveDate         = reader["InitialEffectiveDate"].ToString(),
                        IsAdministrativeServicesOnly = reader["IsAdministrativeServicesOnly"].ToString(),
                        EffectiveProvision           = reader["EffectiveProvision"].ToString(),
                        ProbationPeriod              = reader["ProbationPeriod"].ToString(),
                        UnderwritingType             = ExtractUnderwritingType(id),
                        ProbationPeriodUnit          = reader["ProbationPeriodUnit"].ToString(),
                        StateOfIssue                 = reader["StateOfIssue"].ToString(),
                    };
                    BenefitData benefitData = new BenefitData();
                    eligibilityClassBenefit.Benefit = benefitData.ExtractBenefit(reader, id, classCode);

                    EligibilityClassBenefitBusinessLevelData eligibilityLevelData = new EligibilityClassBenefitBusinessLevelData();
                    eligibilityClassBenefit.EligibilityClassBenefitBusinessLevelNodes = eligibilityLevelData.ExtractBenefitBusinessLevel(reader);

                    eclassBene.Add(eligibilityClassBenefit);
            }
            return eclassBene;
        }
        catch (Exception e)
        {
            throw new Exception(e.InnerException.Message + e.InnerException.StackTrace);
        }
        finally
        {
            //if (_connection.State == ConnectionState.Open) _connection.Close();
            if (reader != null) reader.Close();
            _command.Dispose();
        }
    }
捕获任何异常并重新将其挤出以进行处理。如果调用代码未处理异常,即未将调用包装在try-catch块中,则该异常将被运行时视为未处理

实际上,做你正在做的事情毫无意义。除非您打算对此问题采取措施,否则不要捕获异常。您在这里所做的只是搞乱调用者的堆栈跟踪,因为您正在重新引用一个新的异常。如果您觉得出于某种原因必须切入并重新收缩,您应该这样做:

catch (Exception e)
{
    throw new Exception(e.InnerException.Message + e.InnerException.StackTrace);
}
请注意,异常实例是在rethrown异常的构造函数中传递的。这将最终成为内部异常

关于您的异常策略,这也是非常不必要的:

catch (Exception e)
{
    throw new Exception("I have a good reason for interrupting the flow", e);
}
ExecuteReader
方法已在连接关闭时抛出一个比您的thr更具体的
InvalidOperationException
if (_connection.State == ConnectionState.Open)
    reader = _command.ExecuteReader(CommandBehavior.CloseConnection);
else
    throw new Exception("Connection Closed");