本文介绍了它是不好的做法,从你的方法返回异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很多的code基方法,我的同龄人写的捕捉,通知和记录自己进行错误处理,一般。

A lot of the code base methods my peers have written perform their own error handling, usually by catching, notifying, and logging.

在这种情况下,方法返回一个布尔值,指示成功或失败。

In these cases the methods return a boolean, indicating success or failure.

虽然有时,如果一个方法失败,我想调用code知道为什么,并返回一个布尔值是不够的。

Sometimes though, if a method fails, I want the calling code to know why, and returning a boolean is not enough.

解决此问题的方法是保持错误处理的方法,但使该方法无效,并有方法抛出的异常。

One way around this is to keep the error handling in the methods, but make the method void, and have the methods throw their Exceptions.

不过,最近我已经坐上了返回适当的方法,这本来是无效的,但在这里我想知道的动作状态,通常的操作方法例外的习惯。

However, recently I've got into the habit of returning Exceptions in appropriate methods, usually action methods that could otherwise be void, but where I'd like to know the status of the action.

返回一个异常在具有空隙的方法抛出异常的优点是,我觉得它更容易为其他程序员使用你的库,和,更重要的是,它意味着你可以安全地调用该方法不不必担心捕捉异常

The advantages of returning an Exception over having a void method throw an Exception is that I think it makes it easier for other programmers to use your library, and, more importantly, it means you can safely call that method without having to worry about catching the Exception

例如,如果方法仅仅是无效的,它可能不会立即明显,程序员应该处理的成功或失败。

For example, if the method is just void, it might not be instantly obvious that the programmer should handle success or failure.

但是,如果方法特别指定异常的返回类型,那么你知道你可以检查的成功或失败,如果你想。但是,这也意味着你不必担心醒目的错误,如果你不想。

But if the methods specifically specifies a return type of Exception, then you know you can check success or failure if you want to. But it also means you don't need to worry about catching the error if you don't want to.

这是否有意义吗?也许我没有使用最好的例子,,但一般情况下,它是确定返回的异常,或是否有更好的方式了吗?

Does that make sense? Maybe I haven't used the best example, but generally, is it OK to return Exceptions, or is there a better pattern out there?

欢呼声

更新

哇,压倒性的结果是没办法。我是这么想的。我必须说,这样做(返回除外)的还挺解决的问题,但它确实感觉不对劲了。

wow, the overwhelming result is no way. I thought so. I must say, doing it (returning an Exception) kinda solved a problem, but it did feel wrong.

因此​​,从一些答案,这些具体的方案的最佳解决方案中(络合物类,或一类具有一个或多个外部的依赖关系(即web服务))似乎是一个定制的结果类?

So, from some of your answers, the best solution to these specific scenarios (a complex class, or a class with one or more external dependencies (i.e. web service)) seems to be a custom results Class?

更新:

真是AP preciating所有的意见,正在通过一切阅读,我仔细考虑所有的输入。

hey guys, really appreciating all the opinions, am reading through everything, and I'm thinking carefully about all the input.

目前我赞成有一个空隙的方法,抛异常,然后抓住他们在外面....是好?

Currently I'm favoring having a void method, throwing the Exceptions, and then catching them on the outside....is that better?

欢呼声

推荐答案

如果你的意思是这样...

If you mean something like ...

public Exception MyMethod( string foo )
{
   if( String.IsNullOrEmpty() )
   {
      return new ArgumentNullException( "foo" );
   }
}

...而不是...

... rather than ...

public void MyMethod( string foo )
{
   if( String.IsNullOrEmpty() )
   {
      throw new ArgumentNullException( "foo" )
   }
}

那么绝对不是,这是不行的做到这一点。您会被完全重新发明例外的目的,并使用它作为HRESULT。这里有一些标准的最佳实践的。

另一个很好的理由不在于标准的代表们将不再匹配方法签名。因此,例如,你不能使用动作<字符串> 上的MyMethod了,并需要使用 Func键<字符串,异常> 代替。

Another good reason not to is that standard delegates would no longer match your method signature. So, for example, you could not use Action<string> on MyMethod anymore and would need to use Func<string,Exception> instead.

@Andy,每个评论,回答太长评论:不是真的。别那么在意调用者。他应该是防守在他的设计呢。想想一个异常的语义......这个应用程序的执行将停止在这里,除非有人知道该怎么办这个问题。如果你能解决的问题,您应该解决这个问题。如果你不能,你需要登陆它,它抛出一个新的水平,因为他们知道该怎么做。

@Andy, per comment, answer too long for a comment: Not really. Don't be so concerned about the caller. He should be being defensive in his design anyway. Think about the semantics of an Exception ... "The execution of this application will stop right here unless somebody knows what to do about this problem." If you can resolve the issue, you should resolve it. If you can't, you have to log it and throw it to the next level, because they may know exactly what to do.

您应该处理你可以处理,扔你不能。根据定义,那家伙调用堆栈拥有世界上比你的更广阔的视野。你的应用程序需要有弹性在它的处理异常和不断前进的能力。他们只有这样,才能做到这一点是防御性的编码和问题推至更高水平的信息,看看是否有什么可以做的。

You should handle what you can handle and throw what you can't. By definition, the guy up the call stack has a broader view of the world than you. Your application needs to be resilient in it's ability to deal with exceptions and keep on going. They only way to do that is defensive coding and push issues up to higher levels of information to see if anything can be done.

目前,如果答案是否的一天结束,然后登录的问题可以理解,是盒子的好公民,并优雅地终止应用程序和生活再战。别太自私和尝试,并隐藏错误您的来电。要防守,他会做同样的。 :)

At the end of the day if the answer is "no", then log the problem to it can be understood, be a good citizen of the box and terminate the application gracefully and live to fight another day. Don't be selfish and try and hide errors for your caller. Be defensive and he will do the same. :)

查看企业库异常处理块。它认为他们真的阐明了如何处理整个体系结构异常的伟大愿景。

Check out the Enterprise Library Exception handling block. It think they really articulate a great vision for how to deal with exceptions throughout your architecture.

这篇关于它是不好的做法,从你的方法返回异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 22:56