在抛出异常后返回 [英] Returning after throwing exceptions
问题描述
在 throw
之后返回
一个值有什么好处吗?如果没有,可以忽略 return
语句,并且它可以删除编译器错误 C4715:并非所有控制路径都返回值$
(示例代码)
$ b$ b
for(ushort i = 0; i if(unitFormation [i] == unit)
{
return unitSetup [i];
}
else
throw unit;
return 0;
无需在异常抛出后返回值。如果你有这个错误,你应该检查你的代码可以到达而不抛出异常的路径,例如
if(something)
throw Exception;
else
返回值;
无法在if的else分支中返回值会导致编译错误,因为根据 something
的值,可能抛出或不抛出异常。
Is it in any way beneficial to return
a value after throw
ing an exception? If not, can the return
statement be left out and is it somehow possible to remove compiler error C4715: not all control paths return a value
?
Thanks in advance.
Edit: (sample code)
for (ushort i = 0; i < itsNumUnits; ++i)
if (unitFormation[i] == unit)
{
return unitSetup[i];
}
else
throw unit;
return 0;
There is no need to return a value after exception throw. If you have this error, you should check the paths your code can get to without throwing an exception, e.g.
if (something)
throw Exception;
else
return value;
Failing to return value in the "else" branch of "if" would cause a compile error because the exception may or may not be thrown depending on value of something
.
这篇关于在抛出异常后返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!