0

this is my insert data class and i want to show the exception error in my windows form like a message box how can i show the exception message?

public void Insert()
{
        try
        {
        objcomm.Connection = objconn;
        objcomm.CommandText = "INSERT INTO goods VALUES(N'" + g_id + "',N'" + g_name + "',N'" + g_qty + "',N'" + b_price + "',N'" + s_price + "',N'" + l_qty + "',N'" + co_id + "')";
        objconn.Open();
        objcomm.ExecuteNonQuery();
        objconn.Close();
        }
        catch(Exception ex)
        {
        GeneralException ex1 = new GeneralException(Resource1.DBError, ex);
        throw ex1;
        }
}  

i want to show the "Resource1.DBError" message when the exception is thrown in message box here:

public partial class WareForm : Form
{
        try
        {
            ware.Insert();
        }
        catch(IndexOutOfRangeException ex)
        {
            MessageBox.Show(ex.Message);
        }
 }
4

1 回答 1

6

您需要捕获抛出的相同类型的异常:

    try
    {
        ware.Insert();
    }
    catch(GeneralException ex)
    {
        MessageBox.Show(ex.Message);
    }
于 2013-04-22T10:57:28.340 回答