在try catch和if else子句中返回语句

时间:2016-07-21 21:39:18

标签: java try-catch

问题是我的代码内部发生了一些奇怪的事情; 让我们来看看我的例子(我清理了一下):

Combined Array
(
  [vehicle] => 2016 Ford Transit 250 Cargo Van
  [stockno] => 153721
  [msrp] => 32195
  [price] => 32195
  [payment] => 359
  [type] => New
  [bodystyle] => Van
  [mileage] => 6
  [year] => 2016
  [make] => Ford
  [model] => Transit 250
  [trim] => Cargo Van
  [store] => Baxter Ford
  [offertype] => $ Off MSRP
  [offervalue] => Up to $10,000
  [disclaimer] => *Valid on in-stock models. Based on stock #161621. Tax, title and license extra. With approved credit. Includes hail sale savings. See dealer for details. Offer expires 8\/1\/16.
  [expires] => 8/1/16
),(
  [vehicle] => 2016 Ford F150 XLT
  [stockno] => 153723
  [msrp] => 36195
  [price] => 36195
  [payment] => 429
  [type] => New
  [bodystyle] => Truck
  [mileage] => 6
  [year] => 2016
  [make] => Ford
  [model] => F150
  [trim] => XLT
)

有一个小函数被另一个函数调用,我们称之为public int foo() throws IOException { if(redirect_url.indexOf("statement_1") != -1){ if(check() == true){ //do something }else{ //do something foo(); //yep, recursive call } }else if(redirect_url.indexOf("statement_2") != -1) { map.clear(); bar(); map.put("val", 1); map.put("val2", "2"); foo(); //yep, recursive call; Remember this line!! }else if(redirect_url.indexOf("statement3") != -1) { return AUTH_SUCCESS; }else if(redirect_url.indexOf("statement4") != -1){ return AUTH_SUCCESS; }else { return AUTH_FAILED; } }catch (Exception e){ return AUTH_FAILED; } return AUTH_FAILED; }

buzz

当我注意到我的代码中存在逻辑错误并发现有趣的事情时,我已经解压缩了我很棒的调试器。

让我们说redirect_url字符串有" statement4" substring,这样第四个elseif子句(不计算内部elseif子句)将进入并返回AUTH_SUCCESS。我是这么想的。

以下是问题,当触发返回AUTH_FAILED时,下一条指令在第二个if if语句内调用foo()函数。我不知道为什么会这样。这么奇怪。和想法?

UPD 1 : 在类中定义的常量: 实施例

public void buzz(){
     try {
         switch (signInAttempt()){
             case AUTH_SUCCESS:
                 //do smth
                 return true;
             case AUTH_FAILED:
                 //do smth
                 return false;
             case ACCESS_REQUEST:
                    //do smth
             default:
                 return false;
             }
      } catch (IOException e) {
            e.printStackTrace();
        }
}

UPD 2 还有一些代码:

满足通话功能

private static final int AUTH_SUCCESS      = 4;

最后见到病人:

public boolean rollIn(){

        try {
            switch (signInAttempt()){
                case AUTH_SUCCESS:
                    //do smth
                case AUTH_FAILED:
                    return false;
                case ACCESS_REQUEST:
                    return true;
                default:
                    return false;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return true;
    }

像这样的东西

1 个答案:

答案 0 :(得分:0)

在您进行递归调用的地方,您应该有return foo();

简要解释它现在如何运作 - 没有return

  1. 第一次调用该方法
  2. 进行递归调用
  3. 递归调用执行并返回一个值,因此我们回到第一次调用的范围
  4. 忽略递归调用返回的值 - 您不会将其分配或返回
  5. 执行继续在if语句之外,没有异常被捕获,所以它转到你的上一个语句return AUTH_FAILED;
  6. 所以,即使递归调用返回AUTH_SUCCESS,第一次调用也会忽略它并返回AUTH_FAILED