3

我有这个代码:

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
            {

            }
        }

但是当关闭连接运行时,我仍然收到这条讨厌的消息:

    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# 控制台应用程序继续运行?

4

1 回答 1

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();
}
于 2013-09-18T15:40:48.990 回答