尝试捕捉方法 [英] try and catch approach

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

问题描述

我有一个简单的问题,如果有人可以回答,那就太好了.

例如,当我在编写一个类时说

Hi I have a simple question and would be nice if someone could answer.

How much is it a good idea for example when I am writing a class say

CSomeClass


,这是一个好主意吗? 我让它的方法抛出异常.

另一方面,可能必须使用此类的人应该将


that I make it''s methods throw exceptions.

Than on the other hand, whoever may have to use this class, should put the
code where CSomeclass is used, in

try{} 

块中使用CSomeclass的代码.

例如,使用CSomeclass的地方

block.

e.g., place where CSomeclass is being used

try
{
CSomeclass b;
b.f1();
...

}
catch (CSomeclass exception)
{

}



谢谢.

还是应该让函数只返回错误值?



thanks.

or should I make the functions just return error values instead?

推荐答案

如果捕获到异常,是否可以将其修复为函数可以像正常一样返回的方式无需调用代码就知道,比是.但是,如果您不能做任何有用的事情来解决这种情况,那么缓存异常的价值是什么?

另外,在这里看看:
.NET中的异常处理最佳实践 [
If you catch the exception, can you fix it in such a way that the function will return like normal without the calling code having to know about it, than yes. But if you cannot do anything useful to resolve the situation, what is the value of caching the exception?

Also, have a look here:
Exception Handling Best Practices in .NET[^]

Good luck!


这不能一口气回答,因为它通常是基于类的使用情况,有时还取决于体系结构的个人观点.

我个人认为,如果它在同一层中,则可以扔掉它,但是如果它跨越了层边界,即从数据层到业务层或从业务层到UI层(在标准3层体系结构中),那么最好在类中处理它,然后返回适当的错误消息.

就像我说的那样,这是一个值得商topic的话题,如果您在Google上搜索抛出与捕获异常",它将为您返回大量的阅读材料.

希望有帮助或至少能提供一些指导.

谢谢
Milind
This can not be answered in one line as it is generally based on circumstances of how the class is being used and sometimes on the architecture''s personal view-point.

I would personally think if it is in same layer you may throw it but in case if it going across layer boundary i.e. from data layer to business layer or from business layer to UI layer (in standard 3-tier architecture), then its better to handle it in the class and then return appropriate error message.

As I said, this is a debatable topic, and if you search on Google for "throwing vs catching exception" it will return you good amount of reading material.

Hope that helps or atleast provides some direction.

Thanks
Milind


没有明确的答案,但请记住以下几点:

*如果预期会出现错误并且可以轻松处理,则最好在返回代码中完成.

*如果在构造函数中发生错误,则应抛出错误,因为无法返回错误代码.老式的C ++编码实践是分两步进行的,在此过程中,您首先构建然后调用一个单独的Create函数,以获取错误代码.现在不建议使用此样式,建议使用异常处理构造错误.

*您的代码不是唯一可以引发异常的代码.如果您的代码调用中有任何函数或库可能引发异常,则您的类的用户将不得不尝试在某种程度上捕获异常.

*您已经在注释和答案中获得了一些.NET参考.如果未使用托管C ++(例如,.NET中的),则应考虑到异常是函数的退出点,因此应在退出之前释放所有需要释放的资源(动态分配的内存,文件句柄,信号量等).存放在RAII类型的容器中,例如auto_ptr,shared_ptr等.
There are no definitive answers, but here are some things to keep in mind:

* If an error is expected and can be easily handled, this is better done in a return code.

* If an error occurs in a constructor, it should throw, as there is no way of returning an error code. Old-style C++ coding practice was to have a two-stage construction, where you first construct and then call a separate Create function to be able to get an error code. This style is now discouraged, and exceptions are the recommended way of handling errors in construction.

* Your code is not the only one that can throw exceptions. If any functions or libraries your code calls can throw exceptions, the user of your class will have to try to catch exceptions at some level.

* You have been given some .NET references in comments and answers. If you are not using managed C++ (e.g. in .NET), you should consider that exceptions are exit points from functions, so that any resource that needs to be freed (dynamically allocated memory, file handles, semaphores etc) before an exit should be kept in a RAII-type container, like auto_ptr, shared_ptr etc.


这篇关于尝试捕捉方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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