在ASP.NET C#中是否存在在各层之间引发异常的替代方法 [英] Is there is an alternate of throwing Exception between layers in ASP.NET C#

查看:83
本文介绍了在ASP.NET C#中是否存在在各层之间引发异常的替代方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中具有分层体系结构,我正在寻找一种避免在层之间引发异常的方法,因为引发异常总是与某些成本相关联.

是否有其他替代方法.
任何帮助将不胜感激.

I am having layered architecture in my project I am looking for an approach to avoid throwing exception between layers as throwing exceptions is associated with some cost always.

Is there any alternative approach for this.
Any help will be appreciated.

推荐答案

唯一的选择是返回某种错误状态代码.我经常使用类似的代码:

if(!DataLayer.Save(myObject)//如果失败,则告诉用户失败.
The only alternative is to return some sort of error status code. I do often use code like:

if (!DataLayer.Save(myObject) // If it fails, tell the user it failed.


根据您的要求,您可以删除其他任何层上的try/catch而不是UI(自动抛出).直接在表示层中捕获它并相应地显示它.

尽管不建议在所有"情况下使用.一个人应该根据需要放它.

此外,我很久以前读了一篇令人信服的文章....
例外的真实代价 [
From the requirement you are asking, you can just remove try/catch at any layer other than UI (Automatic throw). Directly catch it in presentation layer and accordingly display it.

Though it''s not suggested for ''all'' scenario''s. One should put it based on the need.

Further, i read a convincing article long back....
The True Cost of Exceptions[^]

Try-catch at every place is still not suggested but should not be avoided in the places where it helps.


这篇关于在ASP.NET C#中是否存在在各层之间引发异常的替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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