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();
}
}
上記は、一般的な例外キャッチを含むコードの例ですが、このプログラムを実行すると、.net ランタイム エラー null 参照例外を含むアプリケーション ログにランダムに中断し、処理されない例外が記録されます。
少し背景を説明します...これは、アプリケーション サーバー上で真夜中に自動的に実行されるコンソール アプリケーションです。別の SQL Server 2008 ボックスに対してストアド プロシージャを実行します。以前は、メンテナンス タスクを実行しているときに SQL サーバーによって接続が切断されたときにこれらのエラーが発生していましたが、現在はそうではありません。もっと具体的なエラーを取得する必要があります。catch 句をバイパスして、未処理のランタイム例外をスローする理由がわかりません。これは何を意味するのでしょうか?これは、この 1 つだけでなく、コードの任意の数のポイントで発生します。これは、爆発した最後の例にすぎません