如果NullRefs曾经被抓?被抓、NullRefs

2023-09-05 00:34:05 作者:心若向阳无谓伤悲

我最近所作的发言,以一个同事说:

I recently made the statement to a colleague that:

NullReferenceException异常应的永远的   显式捕获

NullReferenceExceptions should never be explicitly caught

我用这个词从来没有....嗯。我从来没有见过一个适当的用例自己追赶他们,但我想检查,如果任何人有?

I used the word never.... hmmm. I've never seen a appropriate use case myself for catching them but I wanted to check if anyone else has?

决不毕竟是这样一个强烈的字眼......

Never is such a strong word after all.....

推荐答案

这取决于原因;看到埃里克利珀的博客条目。 如果他们是愚蠢的例外,则没有 - 只要解决调用code。在极少数情况下,他们是令人烦恼的异常(即code您呼叫有陷阱是难以避免的),那么我想你得。

It depends on why; see Eric Lippert's blog entry. If they are "boneheaded exceptions", then no - just fix the calling code. In the rare case that they are "vexing exceptions" (i.e. the code you are calling has traps that are hard to avoid), then I guess you'd have to.