1

I have written a c# monitoring program to check values in a database. It runs 4 separate checks then outputs the results (Winform).

It was all running great then last week the first check hit a problem and whole program stopped.

So my question is how would I trap any errors in any of the 4 checks and keep going/ trucking?

Can I do it with try catch or would this halt program?

Code Sample

        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);
Ggalla1779
  • 476
  • 7
  • 18
  • Using try-catch will probably be useful, but you need to show some of the code to be able to determine that. – Lars Kristensen Jan 26 '16 at 11:57
  • Whatever you do don't just ignore the error! However having a try / catch will help. Inside the catch block log the error so that at least you can check what errors you're getting and don't throw an exception. If you know what errors may occur then have catch blocks for those specific errors. – sr28 Jan 26 '16 at 11:59
  • Try taking a look at this: https://msdn.microsoft.com/library/seyhszts%28v=vs.100%29.aspx – sr28 Jan 26 '16 at 12:02
  • 1
    Another useful link: http://stackoverflow.com/questions/14973642/how-using-try-catch-for-exception-handling-is-best-practice – sr28 Jan 26 '16 at 12:04
  • that was an excellent answer to the error handling – Ggalla1779 Jan 26 '16 at 13:49

1 Answers1

1

You can use a try catch block with the expected exceptions and a general one, just to be certain you catch them all and not throw the exception, therefore not halting the program.

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);
}

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

jcsmata
  • 68
  • 1
  • 6