异常与布尔值 [英] Exception vs Boolean

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

问题描述

哪种方法更好:

1)使用返回true / false的方法(该方法将使用log4net记录异常

导致false)



2)使用返回void的方法并在发生故障时抛出异常?


如果是第二种方法有人建议:应该.NET预定义

异常,NullReferenceException,IndexOutOfRangeException等,或者我的

会抛出异常吗?


提前致谢,

Shehab。

Which approach is better:
1) Use a method that returns true/false (the method will log the exception
that lead to false using log4net for example)
OR
2) Use a method that returns void and throws an exception in case of failure?

If the second approach is to be suggested: Should .NET predefined
exceptions, NullReferenceException, IndexOutOfRangeException and so on, or my
own exceptions be thrown?

Thanks in advance,
Shehab.

推荐答案

从马口:
http://blogs.msdn.com/kcwalina/archi ... 16 / 396787.aspx


" Shehab Kamal" < Sh ********* @ discussion.microsoft.com写信息

新闻:2F ******************* *************** @ microsof t.com ...

哪种方法更好:

1)使用一种方法返回true / false(该方法将记录异常

,例如使用log4net导致false)

OR

2)使用方法

失败后返回void并抛出异常?


如果建议采用第二种方法:应该预定.NET

例外,NullReferenceException,IndexOutOfRangeException等,或者

我的

抛出自己的异常?


提前谢谢,

Shehab。

From the horse mouth:
http://blogs.msdn.com/kcwalina/archi...16/396787.aspx

"Shehab Kamal" <Sh*********@discussions.microsoft.comwrote in message
news:2F**********************************@microsof t.com...
Which approach is better:
1) Use a method that returns true/false (the method will log the exception
that lead to false using log4net for example)
OR
2) Use a method that returns void and throws an exception in case of
failure?

If the second approach is to be suggested: Should .NET predefined
exceptions, NullReferenceException, IndexOutOfRangeException and so on, or
my
own exceptions be thrown?

Thanks in advance,
Shehab.


我真的相信我应该抛出自己的自定义异常但是

文章说:不要创建并抛出新的例外只是为了让你的

团队的?异常。

您是否有更多关于抛出自定义异常的资源?什么时候和什么时候不使用它们?


提前谢谢,

Shehab。
I''m really convinced that I should throw my own custom exceptions but the
article sates that "Do not create and throw new exceptions just to have a??your
team''sa?? exception".
Do you have more resources about throwing custom exceptions? When and when
not to use them?

Thanks in advance,
Shehab.


在CLR性能方面例外是昂贵的,所以,你应该

尽可能避免扔掉它们。


如果你决定如果你需要一个例外,你当然可以使用

内置异常类型,如果它们适合你的情况,或者你当然可以使用一个自定义异常来创建一个自定义异常内置异常类。

" Shehab Kamal" < Sh ********* @ discussion.microsoft.com写信息

新闻:2F ******************* *************** @ microsof t.com ...
Exceptions are expensive in terms of CLR performance and so, you should
avoid throwing them when you can.

If you decide that an exception is what you need, you can certainly use the
built-in exception types if they fit your situation or you can certainly
create a custom exception based on one of the built-in exception classes.
"Shehab Kamal" <Sh*********@discussions.microsoft.comwrote in message
news:2F**********************************@microsof t.com...

哪种方法更好:

1)使用返回true / false的方法(该方法将使用log4net记录异常

导致false)

OR

2)使用返回void的方法并在

失败的情况下抛出异常?


如果建议采用第二种方法:应该.NET预定义

异常,NullReferenceException,IndexOutOfRangeException等等,或者

我的

抛出自己的异常?

提前致谢,谢谢哈!
谢哈布。
Which approach is better:
1) Use a method that returns true/false (the method will log the exception
that lead to false using log4net for example)
OR
2) Use a method that returns void and throws an exception in case of
failure?

If the second approach is to be suggested: Should .NET predefined
exceptions, NullReferenceException, IndexOutOfRangeException and so on, or
my
own exceptions be thrown?

Thanks in advance,
Shehab.



这篇关于异常与布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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