如何处理ibm.data.db2.iseries异常

时间:2012-12-13 04:11:20

标签: c# db2

我有这段代码:

con = new iDB2Connection(connectString);
try { con.Open(); }
catch (iDB2ConnectionTimeoutException ex)
{ Console.WriteLine(ex.Message); }
catch (iDB2DCFunctionErrorException ex)
{ Console.WriteLine(ex.Message); }
catch (AccessViolationException ex)
{ Console.WriteLine(ex.Message); }

关闭连接

if (con != null)
{
            try
            {
                Console.WriteLine("CRASH IS AFTER THIS");
                if (con.State != ConnectionState.Closed)
                {
                    con.Close();
                }
            }

            catch (iDB2ConnectionTimeoutException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (iDB2DCFunctionErrorException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (AccessViolationException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (iDB2Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {

            }
        }

但是在运行Close连接时,我仍然会收到这条令人讨厌的消息:

    Unhandled Exception: IBM.Data.DB2.iSeries.iDB2DCFunctionErrorException: An unexp
ected exception occurred.  Type: System.AccessViolationException, Message: Attem
pted to read or write protected memory. This is often an indication that other m
emory is corrupt.. ---> System.AccessViolationException: Attempted to read or wr
ite protected memory. This is often an indication that other memory is corrupt.
   at IBM.Data.DB2.iSeries.CwbDc.DcDnIsAlive(Int32 functionNumber, IntPtr connec
tionHandle, IntPtr nullParm)
   at IBM.Data.DB2.iSeries.MPConnection.IsAlive()
   --- End of inner exception stack trace ---
   at IBM.Data.DB2.iSeries.MPConnection.IsAlive()
   at IBM.Data.DB2.iSeries.MPConnectionManager.GetConnection(iDB2Connection piDB
2Connection)
   at IBM.Data.DB2.iSeries.iDB2Connection.Open()

当我知道iSeries因维护而停机时,我就明白了 我该如何处理这个以便C#控制台应用程序继续?

1 个答案:

答案 0 :(得分:0)

为什么不将它放在Finally块中并只使用一次try / catch?

con = new iDB2Connection(connectString);

try 
{ 
    con.Open(); 
}
catch (iDB2ConnectionTimeoutException ex)
{ 
    Console.WriteLine(ex.Message); 
}
catch (iDB2DCFunctionErrorException ex)
{ 
    Console.WriteLine(ex.Message); 
}
catch (AccessViolationException ex)
{ 
    Console.WriteLine(ex.Message);
} 
finally
{
    if (con != null && con.State == ConnectionState.Open)
            con.Close();
}