这是我的插入数据类,我想在我的Windows窗体中显示异常错误,如消息框,如何显示异常消息?
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;
}
}
我想在消息框中抛出异常时显示“Resource1.DBError”消息:
public partial class WareForm : Form
{
try
{
ware.Insert();
}
catch(IndexOutOfRangeException ex)
{
MessageBox.Show(ex.Message);
}
}
答案 0 :(得分:6)
您需要捕获引发的相同类型的异常:
try
{
ware.Insert();
}
catch(GeneralException ex)
{
MessageBox.Show(ex.Message);
}