我们如何和在哪里编写try catch块来处理异常 [英] How and where do we write try catch block to handle Exception

查看:127
本文介绍了我们如何和在哪里编写try catch块来处理异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用C#语言来开发Windows应用程序。

We are using C# language to develope a Windows application.

我们的Windows应用程序由三层(UI,Business和DataAccess层)组成。在业务层中,有一些公共(业务)方法,UI通过它们与业务层类进行通信。这些公共方法也有一些私有的方法来实现所需的功能。 DataAcess层中有一些从业务层类调用的方法。

Our windows application consists of three layers (UI,Business and DataAccess layer). In Business Layer there are some public (business) methods through which UI communicates wilh Business layer classes. These public methods also have some private methods to implement the required functionality. There are some methods in DataAcess layer which are called from Business layer class.

在这种情况下,我应该在哪里尝试?
a)在业务层公共方法
b)在忙碌层私有方法
c)在DataAccess层方法
d)在调用业务方法的UI方法中。

In this situatuion where should i wrte try-catch? a) In Business Layer Public methods b) In Busyness Layer Private methods c) In DataAccess Layer methods d) In UI methods from where Business methods are called.

推荐答案

只有当您期望他们并希望用特定的方式做某事时,才应该处理异常。否则,IMO,最好让它们起泡层,以便您有一个完整的堆栈跟踪。即使在演示层,我也会倾向于让错误出现泡沫(即,转到友好的错误屏幕),除非是我特别期待的例外。

You should only handle exceptions when you expect them and want to do something with specific ones. Otherwise, IMO, it is better to let them bubble up the layers so you have a complete stack trace. Even in the presentation tier, I'd be inclined to let errors bubble up (i.e. go to a friendly error screen) unless it was an exception I was specifically expecting.

具有例外情况的一般规则是检查您的输入,以避免它们,预期您期望获得的输入,并让其他所有内容都是异常的,并发出错误。错误和堆栈跟踪是一件好事。他们让你知道一些错误,哪里出了问题。如果给定的层掩盖了错误,几乎不可能确定出了什么问题。

The general rule with exceptions is to check your inputs in an effort to avoid them, anticipate ones you expect to get and let everything else be exceptional and throw an error. Errors and stack traces are a good thing. They let you know something is wrong and where it went wrong. If a given layer buries its errors, it becomes almost impossible to determine what went wrong.

这篇关于我们如何和在哪里编写try catch块来处理异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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