【问题标题】:What is the meaning of Unreacheable Statement [duplicate]Unreachable Statement是什么意思[重复]
【发布时间】:2018-09-28 04:02:51
【问题描述】:

上面写着“int add = t+r;”是一个无法到达的声明。那是什么意思?如何纠正?

public class parseMETHOD {
       public static void main(String[] args) {
           int a=9;
           int b=45;
           int result=calFunction(a,b);
           System.out.println(result);
       }

       private static int calFunction(int t,int r) {
           throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools |
   Templates.
           int addition = t+r;
           return addition;

       } }

【问题讨论】:

  • calFunction() 中,你在函数的开头抛出了一个错误,所以下面的语句永远不会被调用,即它们无法被访问。您可以在抛出错误之前添加条件检查,以便可能到达以下语句。
  • 在 return ,throw 语句之后什么都不执行。

标签: java methods unreachable-statement


【解决方案1】:

由于您的某些程序的控制,该语句在逻辑上无法访问 流动。您正在抛出一个未在您的函数中捕获的异常。

请删除 throw 语句和“模板”一词

   public class parseMETHOD {
       public static void main(String[] args) {
           int a=9;
           int b=45;
           int result=calFunction(a,b);
           System.out.println(result);
       }

       private static int calFunction(int t,int r) {

           int addition = t+r;
           return addition;

       }
   }

此外,良好的命名约定规定您应该将类​​名大写。

【讨论】:

  • 非常感谢。成功了!
猜你喜欢
  • 1970-01-01
  • 2023-04-10
  • 2020-04-22
  • 1970-01-01
  • 2011-01-09
  • 2010-11-05
  • 1970-01-01
  • 2015-09-25
  • 1970-01-01
相关资源
最近更新 更多