删除死代码会导致错误(Removing dead code leads to error)

系统教程 行业动态 更新时间:2024-06-14 16:57:40
删除死代码会导致错误(Removing dead code leads to error)

我使用Eclipse 4.3 Kepler(实际上是STS 3.6.1)。

我遇到了一些代码:

private String someMethod(String myParam) { try { MyInterface myVar = (MyInterface) domeSomething(myParam); if (myVar != null) { return myVar.methodThatReturnsString(); } } catch (Exception e) { return ""; } return ""; // eclipse marks this as dead code }

(正如你所期望的那样, doSomething()方法会抛出一些异常,并且它会返回一个比MyInterface更普遍的接口。)

Eclipse将最后一个返回语句强调为死代码 ,并且如果我按照quickfix建议将其删除,那么我和“此方法应该返回String类型的结果”错误。

为什么最后一个返回语句的死代码? 是因为班级演员? 说doSomething()可以返回null,如果你投了它,会抛出一个类抛出异常吗?

而且,Eclipse为什么建议我修复导致死代码警告的错误? 是否因为Eclipse无法预测这一点?

I'm using Eclipse 4.3 Kepler (actually STS 3.6.1).

I ran into some code:

private String someMethod(String myParam) { try { MyInterface myVar = (MyInterface) domeSomething(myParam); if (myVar != null) { return myVar.methodThatReturnsString(); } } catch (Exception e) { return ""; } return ""; // eclipse marks this as dead code }

(As you'd expect, the doSomething() method throws some exception, and it returns an interface more general than MyInterface.)

Eclipse underlines the last return statement as dead code, and if I remove it as the quickfix suggests, I and up with the "This method should return a result of type String" error.

Why is the last return statement dead code? Is it because of the class cast? Say that doSomething() could return null, if you cast it, would that throw a class cast exception?

And, why does Eclipse suggest that I fix the error with something that leads to a dead code warning? Is it because Eclipse can't predict this?

最满意答案

您的发布代码中没有死代码。 我能看到的唯一问题是:

if (myVar != null) { return myVar; }

你应该返回一个String时返回一个MyInterface 。 编译器会抱怨它,这是正确的。

另外,作为一个更好的选择,你不应该直接在try或catch块内部返回,而应该在这个块之后设计一个地方来返回结果。 这将使您的代码避免任何死代码编译器错误。 你应该看起来像这样:

private String someMethod(String myParam) { String result = ""; try { MyInterface myVar = (MyInterface) domeSomething(myParam); if (myVar != null) { result = myVar.methodThatReturnsString(); } } catch (Exception e) { //handle the exception //basic handling shown System.out.println("Warning. There was a problem executing someMethod:"); e.printStacktrace(); } return result; }

There's no dead code in your posted code. The only problem I can see is here:

if (myVar != null) { return myVar; }

You're returning a MyInterface when you should return a String. The compiler will complain about it and it's right.

Also, as a better alternative, you should not directly return inside the try or catch block, instead design a single place after this block to return the result. This will make your code avoid any dead code compiler error. Your could should look like:

private String someMethod(String myParam) { String result = ""; try { MyInterface myVar = (MyInterface) domeSomething(myParam); if (myVar != null) { result = myVar.methodThatReturnsString(); } } catch (Exception e) { //handle the exception //basic handling shown System.out.println("Warning. There was a problem executing someMethod:"); e.printStacktrace(); } return result; }

更多推荐

本文发布于:2023-04-13 12:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/af9e8f8d2a662d632b5a7b3ac3559447.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   代码   Removing   dead   leads

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!