5

我想在 Windows 窗体应用程序中创建自己的异常。我正在尝试将一些数据添加到数据库中。

代码:

try
{
    string insertData = string.Format("INSERT INTO " + constants.PIZZABROADCASTTABLE + 
      " VALUES(@starttime,@endtime,@lastupdatetime,@applicationname)");
    sqlCommand = new SqlCommand(insertData, databaseConnectivity.connection);
    sqlCommand.Parameters.AddWithValue("@starttime", broadcastStartDateTime);
    sqlCommand.Parameters.AddWithValue("@endtime", broadcastEndDateTime);
    sqlCommand.Parameters.AddWithValue("@lastuptime", currentDateTime);
    sqlCommand.Parameters.AddWithValue("@applicationname", txtApplicationName.Text);
    sqlCommand.ExecuteNonQuery();
}
catch (DataBaseException ex)
{
    MessageBox.Show(ex.Message);
}

在这里,我创建了自己的异常。在这里,我给出了标量变量@lastuptime而不是@lastupdatetime用于捕获 SqlException。

这是我的 DatabaseException 类。

class DataBaseException : Exception
{
    public DataBaseException(string Message)
        : base(Message)
    {

    }
    public DataBaseException(string message, Exception innerException)
        : base(message, innerException)
    {
    }
}

这里在运行程序时显示错误

 sqlCommand.ExecuteQuery();

但它不会捕获错误并显示消息框文本。我知道我做错了什么。我不知道我创建的自定义异常处理是对还是错。

谁能帮我?提前致谢。

4

5 回答 5

2

您需要抛出您的自定义异常,以便它可以被调用方法捕获。在您的代码中,程序将从数据库中抛出异常,而不是您的自定义异常。

void UpdateDatabase()
{
//...
        try 
            { 

            } 
               // Your checks to identify - type of exception
               // ...
              // .net exception
              // Throw your custom exception in the appropriate block - 
              // throw new DatabaseException();
            catch (OutOfMemoryException ex1) 
            { 

            }
            catch (InvalidDataException ex2) 
            { 

            }
            // Generic exception
            catch (Exception ex3) 
            { 
                // throw new DatabaseException();
            } 
//....
}

// Method calling UpdateDatabase need to handle Custom exception
void CallUpdateDatabase()
{
 try
  {
    UpdateDatabase();
  }
  catch(DatabaseException dbEx)
  {
    // Handle your custom exception
  }
}
于 2011-09-16T05:35:41.973 回答
0

SqlCommand 属性和方法都不会引发您创建的 DatabaseException。因此,您的捕获将永远不会触发。

您创建并调用了异常 DatabaseException 的事实并不意味着所有“数据库”代码现在都会抛出您的异常。在编写 SqlCommand 时,它被编写为抛出一组非常特定的异常。您可以在 MSDN 上找到 SqlCommand 及其方法抛出的异常列表。

如果我的回答对您没有意义,请告诉我。

于 2011-09-16T05:40:32.780 回答
0

您自己的异常很好,但 Sql.ExecuteQuery 会抛出 SqlException,您可以执行以下操作:

void CallDatabase( /* your parameters */)
{
    try
    {
        string insertData = string.Format("INSERT INTO " + constants.PIZZABROADCASTTABLE + " VALUES(@starttime,@endtime,@lastupdatetime,@applicationname)");
        sqlCommand = new SqlCommand(insertData, databaseConnectivity.connection);
        sqlCommand.Parameters.AddWithValue("@starttime", broadcastStartDateTime);
        sqlCommand.Parameters.AddWithValue("@endtime", broadcastEndDateTime);
        sqlCommand.Parameters.AddWithValue("@lastuptime", currentDateTime);
        sqlCommand.Parameters.AddWithValue("@applicationname", txtApplicationName.Text);
        sqlCommand.ExecuteNonQuery();

    }
    catch (SqlException ex)
    {
        throw new DataBaseException("Database error", ex);
    }
}

/* somewhere in your code */
try
{
   CallDatabase(...);
}
catch (DataBaseException ex)
{
    MessageBox.Show(ex.Message);
}
于 2011-09-16T05:41:44.823 回答
0

这就是我编写这个例程的方式(或多或少)。确保关闭数据库连接并使用using构造释放sqlCommand obj:

 try
    {
        string insertData = string.Format("INSERT INTO " + constants.PIZZABROADCASTTABLE + " VALUES(@starttime,@endtime,@lastupdatetime,@applicationname)");
        using (sqlCommand = new SqlCommand(insertData, databaseConnectivity.connection))
        {
            sqlCommand.Parameters.AddWithValue("@starttime", broadcastStartDateTime);
            sqlCommand.Parameters.AddWithValue("@endtime", broadcastEndDateTime);
            sqlCommand.Parameters.AddWithValue("@lastuptime", currentDateTime);
            sqlCommand.Parameters.AddWithValue("@applicationname", txtApplicationName.Text);

            sqlCommand.ExecuteNonQuery();
        }
    }
    catch (Exception ex)
    {
        string s = "Failed to insert into table " + constants.PIZZABROADCASTTABLE + "Database Error! " + Environment.NewLine + "Details: " + ex.ToString();
        MessageBox.Show(s, MessageBoxButtons.OK, MessageBoxIcons.Error);
        // Or
        //throw new DatabaseException(s, ex);
    }
    finally
    {
        if (databaseConnectivity != null && databaseConnectivity.connection != null) 
            databaseConnectivity.connection.Close();
        else
            MessageBox.Show("No database connectivity!", "Error", MessageBoxButtons.OK, MessageBoxIcons.Error); 
    }
于 2011-09-16T06:18:39.767 回答
0

使用 Throw Class ,更多信息在这里。

http://msdn.microsoft.com/en-us/library/system.activities.statements.throw.aspx

问候。

于 2011-09-16T06:26:23.760 回答