处理错误代码 [英] Handling Error Codes

查看:128
本文介绍了处理错误代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在我的应用程序发生异常时,我需要根据我需要的错误代码将错误代码从业务层

传递到Presentation层显示数据库中可用的消息。

我想知道如何传递并获取表示层中的错误代码。

用于记录我正在使用的异常log4net和企业库4.0。



提前致谢

Hi,
In my application when exception occurs i need to pass the Error Code from the Business Layer
to the Presentation layer here based on the error code i need to show the message that is available in the DB.
I want to know how to pass and get the Error code in the Presentation Layer.
For logging Exceptions i am using log4net and Enterprise library 4.0.

Thanks in advance

推荐答案

使用您自己的例外。创建从System.Exception或System.ApplicationException或其他继承的适当异常类,并添加您的ErrorCode属性。在业务层中适当时抛出异常。在表示层中,你需要一些try ... catch块,当你捕获一个特定的异常时,你可以访问它的错误代码并获得它的措辞。

注意你可以尝试后有多个catch块,最具体的例外必须先行:

Use your own Exceptions. Create appropriate exception classes inheriting from System.Exception or System.ApplicationException or other, and add your ErrorCode property. Thrown the exception when appropriate in the Business Layer. In the Presentation Layer, you need some try...catch blocks, and when you catch one of your specific exceptions, you can access its error code and get the wording for it.
Note that you can have multiple catch blocks after a try, the most specific exception has to come first:
try
{
    ...some code
}
catch (MyException mex)
{
    string msg = GetMessage(mex.ErrorCode);
    ...
}
catch (System.Exception ex)
{
    ...
}


这篇关于处理错误代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆