【问题标题】:looping over a try/catch block?循环一个 try/catch 块?
【发布时间】:2013-12-06 01:58:25
【问题描述】:

我正在尝试编写如下所示的 try catch 块,但将其放入循环中。我的问题是,当我把它放在一个while循环中时,它会运行x次。我希望它在第一次尝试成功时停止。但可以选择最多运行 3 次。

    try {

            myDisplayFile();

        } catch (FileNotFoundException e1) {
            System.out.println("could not connect to that file..");

            e1.printStackTrace();
        }

    public static void  myDisplayFile() throws FileNotFoundException{
    Scanner kin = new Scanner(System.in);
    System.out.print("Enter a file name to read from:\t");
    String aFile = kin.nextLine();

    kin.close();

    Scanner fileData = new Scanner(new File(aFile));
    System.out.println("The file " + aFile + " contains the following lines:");

    while (fileData.hasNext()){
        String line = fileData.next();
        System.out.println(line);
    }//end while
    fileData.close();
}

【问题讨论】:

    标签: java exception try-catch


    【解决方案1】:
    int max_number_runs = 3;
    boolean success = false;
    
    for( int num_try = 0 ; !success && num_try < max_number_runs ; num_try++ )
    {
        try
        {
            /* CODE HERE */
            success = true;
        }
        catch( Exception e )
        {
    
        }
    }
    

    【讨论】:

      【解决方案2】:
      int someCounter = 0;
      boolean finished = false;
      while(someCounter < 3 && !finished) {
          try {
              //stuff that might throw exception
              finished = true;
          } catch (some exception) {
              //some exception handling
              someCounter++;
          }
      }
      

      您可以在 try catch 块中使用 break;,它会退出它所在的循环(不仅仅是 try catch 块),但从可读性的角度来看,这个发布的代码可能会更好。

      finished = true 应该是try 块的最后一行。它不会抛出异常。它只会在try 块的每一行没有exception 的情况下执行。所以如果你到达finished = true,你没有抛出异常,所以你可以切换你的标志并退出循环。

      否则,如果抛出异常,finished = true; 行将不会执行。您将处理异常,然后递增 someCounter++ 变量。

      从可读性的角度来看这是理想的,因为所有可能的while 循环退出都标记在while 循环的条件部分。循环将继续,直到 someCounter 太大,或 finished 返回 true。因此,如果我正在阅读您的代码,我所要做的就是通过您的循环查看这些变量被修改的部分,即使我还不了解循环所做的一切,我也可以快速理解循环逻辑.我不必寻找break; 声明。

      【讨论】:

        【解决方案3】:

        我希望我正确理解了您的问题。这是一种方法。

            int noOfTries = 0;
            boolean doneWithMyStuff = false;
            final int MAX_LOOP_VAL = 10;
            int noOfLoops = 0;
        
            while(noOfTries < 3 && noOfLoops < MAX_LOOP_VAL && !doneWithMyStuff) {
        
                try {
        
                    // Do your stuff and check success
        
                    doneWithMyStuff = true;
                }
                catch (Exception e) {
        
                    noOfTries++;
                    e.printStackTrace();
                }
                finally {
        
                    // Close any open connections: file, etc.
        
                }
                noOfLoops++;
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-08-27
          • 1970-01-01
          • 1970-01-01
          • 2011-06-08
          • 1970-01-01
          • 1970-01-01
          • 2013-05-27
          相关资源
          最近更新 更多