包装异常 [英] Wrapping an exception
本文介绍了包装异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果出现任何主键冲突错误而没有说主键违反了GUI,如何从数据访问层向GUI层抛出异常.
How can I throw an exception to the GUI layer from my Data Access layer if any primary key violation error comes without saying primary key violated to the GUI.
推荐答案
您需要从数据层抛出异常,例如:
You need to throw an exception from your datalayer for example :
public void ConnectDatabase()
{
try
{
SqlConnection myConnection = new SqlConnection("connectionstring");
myConnection.Open();
}
catch (SqlException sqlEx)
{
// Include inner exception to give more information about what went wrong
throw new MyCustomException("Failed to open new SQL Connection", sqlEx);
}
}
在Gui层中,您将连接到某个地方的数据库,此时,您需要捕获自定义的异常:
In your Gui layer, you''ll connect to the database somewhere, at that point, you need to catch your custom made exception :
// Application starts
try
{
DataLayerObject myObject = new DataLayerObject();
myObject.ConnectDatabase();
}
catch (MyCustomException customEx)
{
MessageBox.Show(customEx.Message);
}
玩得开心!
爱德华
Have fun!
Eduard
这篇关于包装异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文