2013-04-22 77 views
0

這是我的插入數據類,我想在我的Windows窗體中顯示異常錯誤就像一個消息框我該如何顯示異常消息?C#:拋出異常其他類

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

回答

6

你需要捕捉時引發同一類型的異常:

try 
    { 
     ware.Insert(); 
    } 
    catch(GeneralException ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
+0

非常感謝你 – 2013-04-22 11:00:35