C#中抛出异常后的程序流程
您好,我正在查看一些旧的 C# 代码,并注意到很多这样的代码:
void SomeFunction()
{
if (key.Length != Dimensions)
{
throw new KeySizeException();
}
else
{
SomeOtherFunction();
}
}
我想知道是否存在甚至需要 else 块的情况?我可以安全地将代码缩短为此而不产生任何影响吗?
void SomeFunction()
{
if (key.Length != Dimensions)
{
throw new KeySizeException();
}
SomeOtherFunction();
}
默认情况下,异常应该将程序流抛出此方法,对吧?但我只是想知道 DotNet 中是否有一种方法可以调整未处理异常的处理方式,这会导致第二个实现与第一个实现的工作方式不同?
Hi I'm looking at some old c# code and noticing a lot of code like this:
void SomeFunction()
{
if (key.Length != Dimensions)
{
throw new KeySizeException();
}
else
{
SomeOtherFunction();
}
}
I want to know if there can ever be a case where the else block is even necessary? Can I safely shorten the code to this with no repercussions?
void SomeFunction()
{
if (key.Length != Dimensions)
{
throw new KeySizeException();
}
SomeOtherFunction();
}
By default the exception should throw the program flow out of this method right? But I'm just wondering if there's a way in DotNet to tweak how unhandled exceptions are um handled, that would cause the 2nd implementation to work differently from the first?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您不需要“else”块。这是多余的。如果您使用“Reshaper”或“JustCode' 这样的冗余代码元素通常会被指出。
You do not need the 'else' block. It is redundant. If you use a refactoring tool like 'Reshaper' or 'JustCode' such redundant code elements are usually pointed out.
throw 是该代码块中的显式终端,方法调用将在该点有效结束。这意味着
else
块是多余的,可以删除。The
throw
is an explicit terminal in that code block, the method call will effectively end at that point. This means theelse
block is redundant and can be removed.两者是完全等价的。
The two are completely equivalent.
正如其他人所说,这两段代码是等效的。
不过我想我还有一些额外的想法。
首先,所示代码本质上实现了一个包装方法 (SomeFunction),该方法充当 SomeOtherFunction 的保护子句。我对此会很谨慎 - 当您的 KeySizeException 被捕获时,您将无法仅从堆栈跟踪中得知 SomeOtherFunction 是否参与其中。这也意味着您无法通过对该方法的简单代码检查来了解 SomeOtherFunction 的这一要求。
此外,您可能会考虑将这些类型的代码重新映射到 .Net 4.0 代码契约 - 它们可以使代码更容易阅读。
最后的想法 - 在像你这样的情况下,我有时会想离开其他的。这使得其他人 100% 清楚 if/else 行为是有意的。
As others have said the two pieces of code are equivalent.
I thought I'd some additional thoughts though.
Firstly, the code as shown essentially implements a wrapper method (SomeFunction) that works as a guard clause for SomeOtherFunction. I'd be wary of doing that - when your KeySizeException is caught you will not know just from the stack trace that SomeOtherFunction was involved at all. It also means that you have no visibility of this requirement of SomeOtherFunction through simple code inspection of that method.
Additionally, you might conside refectoring these types of code to .Net 4.0 Code Contracts - they could give much easier to read code.
Final thought - in cases like yours I'm sometimes tempted to leave the else. This makes it 100% clear to someone else that the if/else behaviour is intended.
在 C# 中,两者的工作方式相同。我想你在想如果你处理异常(而不是抛出它),如何摆脱执行第二条语句?
如果你处理并且不想执行SomeOtherFunction,你可以像下面这样返回。
In C# both works in the same way. I think you are thinking if you handle the exception (rather than throwing it), how to get rid of executing the second statement?
If you handle and do not want to execute SomeOtherFunction, you can just return as below.