c# - 捕获错误并继续运输

标签 c# error-handling

我编写了一个 C# 监控程序来检查数据库中的值。它运行 4 次单独的检查,然后输出结果(Winform)。

一切都运行良好,然后上周第一次检查遇到问题,整个程序停止了。

所以我的问题是我将如何在 4 次检查中的任何一个中捕获任何错误并继续/卡车运输?

我可以用 try catch 来做,还是这个停止程序?

代码示例

        bool bTestDate;
        object LastDataDate;

      //--  Check A - Table 1
        OdbcConnection DbConnection = new OdbcConnection("DSN=EDATA");
        try
        {
            DbConnection.Open();
        }
        catch (OdbcException ex)
        {
            Console.WriteLine("connection to the DSN '" + connStr + "' failed.");
            Console.WriteLine(ex.Message);
            return;
        }

        OdbcCommand DbCommand = DbConnection.CreateCommand();
        DbCommand.CommandText = "SELECT NAME, UNAME, DATIME_ENDED FROM XPF WHERE NAME='XXXX'";

        OdbcDataReader DbReader = DbCommand.ExecuteReader();
        int fCount = DbReader.FieldCount;

        string myBatch;
        string myUser;
        string myDate;

        while (DbReader.Read())
        {
            Console.Write(":");

            myBatch = DbReader.GetString(0);
            myUser = DbReader.GetString(1);
            myDate = DbReader.GetString(2);
            myDate = myDate.Remove(myDate.Length - 10);

            for (int i = 0; i < fCount; i++)
            {
                String col = DbReader.GetString(i);
                Console.Write(col + ":");
            }
            tbxxx.Text = string.Format("{0:dd/M/yy   H:mm:ss}", myDate);

            bool TestDate;
            TestDate = CheckDate(Convert.ToDateTime(myDate));
            CheckVerif(TestDate, lblVerifixxx);
        }

      //--  Check B - Table 2
        string cnStr = setConnectionString();
        string mySQL = "Select Max(TO_DATE(TIME_ID, 'DD/MM/YYYY')) FROM table";
        OracleConnection cn = new OracleConnection(cnStr);
        cn.Open();

        OracleCommand cmd = new OracleCommand(mySQL, cn);
        LastDataDate = cmd.ExecuteScalar();
        cmd.Dispose();

        tbLastDate.Text = string.Format("{0:dd MMM yy}", LastDataDate);
        bTestDate = CheckDate(Convert.ToDateTime(LastDataDate));
        CheckVerif(bTestDate, lblVerif);

      //--  Check C - Table 3
        mySQL = "Select Max(xxx_DATE) from AGENT";
        OracleCommand cmd2 = new OracleCommand(mySQL, cn);
        LastDataDate = cmd2.ExecuteScalar();
        cmd2.Dispose();

        tbxxx2.Text = string.Format("{0:dd MMM yy}", LastDataDate);
        bool TestDatex;
        TestDatex = CheckDate(Convert.ToDateTime(LastDataDate));
        CheckVerif(TestDatex, lblVerif2);

最佳答案

您可以使用 try catch阻止预期的异常和一般的异常,只是为了确保您全部捕获它们而不抛出异常,因此不会停止程序。

try
{
    string s = null;
    ProcessString(s);
}
// Most specific:
catch (ArgumentNullException e)
{
    Console.WriteLine("{0} First exception caught.", e);
}
// Least specific:
catch (Exception e)
{
    Console.WriteLine("{0} Second exception caught.", e);
}

查看https://msdn.microsoft.com/en-us/library/0yd65esw.aspx

关于c# - 捕获错误并继续运输,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35013246/

相关文章:

c# - Silverlight 4:将文件上传到服务器

c# - 共享点 "Could not load file or assembly" "The system cannot find the file specified"

c# - Log4Net-SQLite的AdoNetAppender无法插入

objective-c - 对象在 iOS5 上被释放,但在 iOS4 上没有。如何追踪?

java - 为什么错误处理程序不处理我的错误?

c# - 分部类中的表单控件属性

c# - 在标题中使用 '/' 时如何防止列不显示在 WPF 数据网格中?

MySQL 错误 1822 : Failed to add foreign key constraint; missing index for contraint BUT index exists

asp.net - 为什么我在从 Application_Error 和 errorMode ="Custom"执行 Server.Transfer 时看到 2 个错误页面?

python - 如何处理来自谷歌地图 api 的错误?