【发布时间】:2017-02-17 00:59:32
【问题描述】:
下面的示例代码在编译时给出了“使用未分配的局部变量'resultCode'”:
string answer;
string resultCode;
try
{
resultCode = "a";
}
catch
{
resultCode = "b";
}
finally
{
answer = resultCode;
}
我原以为上面的 catch 块应该捕获所有异常,因此在进入 finally 块时不可能取消分配 resultCode。任何人都可以解释一下吗? 谢谢。
编辑:谢谢大家。这个引用文档的答案似乎很好地回答了它:https://stackoverflow.com/a/8597901/70140
【问题讨论】:
-
如果
resultCode = "a";抛出异常怎么办?我知道它不会,但编译器不知道。