【问题标题】:Return boolean with an if inside a try [duplicate]在尝试中返回带有if的布尔值[重复]
【发布时间】:2016-07-28 03:38:36
【问题描述】:

我不知道为什么我不能返回真假。它打印“缺少退货声明”。 我在网上寻找答案,但没有一个能解决我的问题。

public boolean Verification(String SQL) throws IOException{
    try{
        Statement statementCount=connection.createStatement();
        ResultSet results = statementCount.executeQuery(SQL);

        String Cont=results.getString("ContadorFecha");
        int cont=Integer.parseInt(Cont);
        if (cont>=10){
            return true;
        } else {
            return false;
        }

    }catch(SQLException ex) {
            ex.printStackTrace();
            System.out.println("Exception in register: " + ex);
        }

}

【问题讨论】:

  • 您不会在 catch 块中返回任何内容。因此,正如错误所说,缺少 return 语句。
  • “但它们都不能解决我的问题” 抱歉,这完全不正确。这个问题有 数千个 版本,有明确的答案,其中许多就在 SO 上。最简单的搜索给我们this onetry/catchthis oneif/elsethis one 和只有if...

标签: java database jdbc


【解决方案1】:
public boolean Verification(String SQL) throws IOException{
      Boolean flag=false;
        try{
            Statement statementCount=connection.createStatement();
            ResultSet results = statementCount.executeQuery(SQL);

            String Cont=results.getString("ContadorFecha");
            int cont=Integer.parseInt(Cont);
            if (cont>=10){
                flag=true;
            }

        }catch(SQLException ex) {
                ex.printStackTrace();
                System.out.println("Exception in register: " + ex);
            }
        return flag;

    }

【讨论】:

    【解决方案2】:

    只需将其更改为:

    public boolean Verification(String SQL) throws IOException {
        try {
            Statement statementCount = connection.createStatement();
            ResultSet results = statementCount.executeQuery(SQL);
    
            String Cont = results.getString("ContadorFecha");
            int cont = Integer.parseInt(Cont);
            if (cont >= 10) {
                return true;
            }
    
        } catch (SQLException ex) {
            ex.printStackTrace();
            System.out.println("Exception in register: " + ex);
        }
        return false;
    }
    

    【讨论】:

      【解决方案3】:

      由于你的 catch 块,它缺少一个 return 语句。

      你要么需要在那里返回一些东西,要么抛出一个异常。

      【讨论】:

      • 只有一半的答案:如果抛出另一种(未捕获的)异常类型怎么办?
      • > 如果抛出另一种(未捕获的)异常类型怎么办 如果抛出 RuntimeException,在这种情况下您不需要返回(显然)
      • @Stultuske:只需将return 添加到catch 块中即可。如果发生了不是SQLException 的事情,方法抛出,它不会返回
      • @T.J.Crowder nyah ...漫长的一天
      • @911DidBush 忽略我的言论
      【解决方案4】:

      您需要为所有可能的场景提供一个返回语句。

      在这个例子中,似乎是因为这个:

      if (cont>=10){
                  return true;
              } else {
                  return false;
              }
      

      实际上可以替换为:

      return cont >= 10;
      

      但是:如果在此之前抛出异常怎么办?

      try 块结束了,这个被处理了(如果是这种类型的 Exception,无论如何)

      catch(SQLException ex) {
                  ex.printStackTrace();
                  System.out.println("Exception in register: " + ex);
              }
      

      添加一个

      return false;
      

      在您的 catch 块之后,您的所有案例都将被覆盖。 JVM 会知道每个场景要返回什么,它会工作。

      【讨论】:

        猜你喜欢
        • 2011-04-28
        • 2016-11-08
        • 2019-04-14
        • 2015-10-05
        • 2022-11-30
        • 2015-09-01
        • 2021-02-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多