try,catch和finally的确切执行顺序是什么? [英] what is the exact order of execution for try, catch and finally?

查看:177
本文介绍了try,catch和finally的确切执行顺序是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个java代码中,

In this java code,

import java.io.IOException;

public class Copy
{
   public static void main(String[] args)
   {
      if (args.length != 2)
      {
         System.err.println("usage: java Copy srcFile dstFile");
         return;
      }

      int fileHandleSrc = 0;
      int fileHandleDst = 1;
      try
      {
         fileHandleSrc = open(args[0]);
         fileHandleDst = create(args[1]);
         copy(fileHandleSrc, fileHandleDst);
      }
      catch (IOException ioe)
      {
         System.err.println("I/O error: " + ioe.getMessage());
         return;
      }
      finally
      {
         close(fileHandleSrc);
         close(fileHandleDst);
      }
   }

   static int open(String filename)
   {
      return 1; // Assume that filename is mapped to integer.
   }

   static int create(String filename)
   {
      return 2; // Assume that filename is mapped to integer.
   }

   static void close(int fileHandle)
   {
      System.out.println("closing file: " + fileHandle);
   }

   static void copy(int fileHandleSrc, int fileHandleDst) throws IOException
   {
      System.out.println("copying file " + fileHandleSrc + " to file " +
                         fileHandleDst);
      if (Math.random() < 0.5)
         throw new IOException("unable to copy file");

      System.out.println("After exception");
   }
}

我期望的输出是

copying file 1 to file 2
I/O error: unable to copy file
closing file: 1
closing file: 2

但是有时我得到这个预期输出,在其他时候我得到以下输出:

However sometimes I get this expected output and at other times I get the following output:

copying file 1 to file 2
closing file: 1
closing file: 2
I/O error: unable to copy file  

有时甚至此输出:

I/O error: unable to copy file
copying file 1 to file 2
closing file: 1
closing file: 2

是否我得到第一,第二或第三个输出似乎在每次执行期间随机发生。我发现显然谈论的这篇文章同样的问题,但我仍然不明白为什么我有时得到输出1,2或3.如果我理解这个代码正确,输出1应该是我每次得到(异常发生)。如何确保我得到输出1一致,或者能够告诉我什么时候会得到输出1或当我将
得到输出2或3?

and whether I get the first, second or third output seems to happen randomly during every execution. I found THIS POST that apparently talks about the same problem, but I still don't understand why I sometimes get output 1, 2 or 3. If I understand this code correctly then output 1 should be what I get every time (the exception occurs). How do I ensure that I get output 1 consistently, or be able to tell when I will be getting output 1 or when I will be getting output 2 or 3?

推荐答案

问题是您正在将一些输出写入 System.out ,有些则输出到 System。 ERR 。这些是独立的流,具有独立的缓冲。根据我的了解,他们被刷新的时间是没有指定的。

The issue is that you are writing some output to System.out and some to System.err. These are independent streams, with independent buffering. The timing of when they are flushed is, as far as I know, not specified.

其中的不足之处在于,写入不同的流时,不能使用该命令其中显示输出来确定调用 println()的顺序。请注意,输出到 System.out 始终按预期顺序显示。

The short of it is that when writing to different streams, you cannot use the order in which the output shows up to determine the order in which the calls to println() occurred. Note that the output to System.out always appears in the expected order.

就执行顺序而言,首先执行 try 的正文。如果它抛出异常,则执行相应的 catch 子句的正文。 finally 块总是最后执行。

As far as order of execution, the body of the try is executed first. If it throws an exception, the body of the appropriate catch clause is then executed. The finally block is always executed last.

这篇关于try,catch和finally的确切执行顺序是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆