为什么必须在catch块中的throw语句之前必须返回return语句 [英] why must return statement precede a throw statement in a catch block

查看:218
本文介绍了为什么必须在catch块中的throw语句之前必须返回return语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码会抱怨

try
{
    session.Save(obj);
    return true;
}
catch (Exception e)
{
    throw e;
    return false;  // this will be flagged as unreachable code
}

,而不会:

try
{
    session.Save(obj);
    return true;
}
catch (Exception e)
{
    return false;
    throw e;
}

我不明白...我以为我的csc101告诉我return语句应该始终是函数中的最后一条语句,并且它退出该函数并将控制权返回给调用代码。为什么这会违抗我教授的逻辑,为什么只有其中一个会发出警告?

I dont get it...I thought my csc101 told me that return statements should always be the last statement in a function and that it exits the function and return control to the calling code. Why does this defy my professor's logic, and why does only one of these generate a warning?

推荐答案

return 将退出该方法;假设 throw try 之内,也会退出该方法。它只能退出一次!

return will exit the method; throw will also exit the method, assuming it is not inside the try. It can only exit once!

因此,无论顺序如何-抛出 / 返回有效地结束了该方法。

So regardless of the order - the first of the throw / return effectively end the method.

不过,作为更一般的反馈:如果目的是在失败时返回false,则您需要做的就是:

As more general feedback, though: if the intent is to return false upon failure, all you need is:

try
{
    session.Save(obj);
    return true;
}
catch
{
    return false;
}

我个人认为这是错误的代码-它向调用者隐藏了实际的问题,使其很难调试。它没有告诉我们为什么失败的任何信息。我会说,更好的方法只是让异常气泡。在那种情况下,返回 true 是没有意义的,因为我们永远不会返回 false -并且没有捕捉点只是为了重新抛出异常。因此整个方法变为:

Personally, I would say that this is bad code - it hides the actual problem from the caller, making it very hard to debug. It tells us nothing of why it failed. I would say that the better approach is simply to let the exception bubble. In that case, there is no point returning true, because we would never return false - and there is no point catching an exception just to re-throw it. So the entire method becomes:

session.Save(obj);

(无需任何其他操作)

如果您的问题是为什么只有其中一个会发出警告:这是一个公平的问题,但是不需要 编译器为您找到其中的 个。也许应该发现它。我怀疑 gmcs 会发现并发出警告-Mono中的编译器更愿意指出

If your question is "why does only one of these generate a warning": a fair question, but the compiler isn't required to spot either of them for you. Perhaps it should spot it. I suspect that gmcs would spot this and warn about it - the compiler in mono is far more willing to point out stupidity.

编辑:按预期,[g] mcs输出:

as expected, [g]mcs outputs:


Program.cs(15,13):警告CS0162:检测到无法访问的代码

Program.cs(15,13): warning CS0162: Unreachable code detected

Program.cs(28,13):警告CS0162:检测到无法访问的代码

Program.cs(28,13): warning CS0162: Unreachable code detected

用于以下代码-因此它确实将两种用法都报告为警告:

for the code below - so it does indeed report both uses as warnings:

class Program
{
    static void Main() { }
    static void DoSomething() { }
    bool ReturnFirst()
    {
        try
        {
            DoSomething();
            return true;
        }
        catch
        {
            return false;
            throw; // line 15
        }
    }
    bool ThrowFirst()
    {
        try
        {
            DoSomething();
            return true;
        }
        catch
        {
            throw;
            return false; // line 28
        }
    }
}

这篇关于为什么必须在catch块中的throw语句之前必须返回return语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆