我在C#4.0中有一个像这样的方法:
protected override bool _update()
{
if (this.Notes == "")
throw new Exception("Some message...");
else
base._update();
}
编译器抱怨并非所有代码路径都返回一个值,但是,如果我这样做:
protected override bool _update()
{
throw new Exception("Some message...");
}
它编译好了。是否有推荐的解决方法?看起来他们真的没有那么不同,如果一个案例中的例外就没有回报价值,为什么不会在另一个案例中呢?
答案 0 :(得分:4)
试一试:)
protected override bool _update()
{
if (this.Notes == "")
throw new Exception("Some message...");
else
return base._update();
}
你只需要返回base._update()的输出,仅仅运行它是不够的,因为不会返回该值。
答案 1 :(得分:1)
该方法期望boolean类型的返回值,但是你没有在这里返回任何东西。无论如何,“例外”案例将退出该职能。您需要执行以下操作:
如果(..) .. 其他 { return base._update(); }
答案 2 :(得分:0)
因为如果this.Notes != ""
,那么您的方法将退出而不返回值。你是说这个吗?
protected override bool _update()
{
if (this.Notes == "")
throw new Exception("Some message...");
else
return base._update();
}
答案 3 :(得分:0)
这是因为你没有这样做 “return base._update” 还请坚持使用标准命名约定作为建议:)
如果stmt问题包括
,请远离悬空protected override bool Update() { if (this.Notes == "") { throw new Exception("Some message..."); } else { return base.Update(); } }