当我在java中启动我的个性化异常时,为什么解析器不遵循我的代码顺序?

时间:2021-08-04 23:26:52

With this code:

使用此代码:

class SimpleException extends Exception {}

public class SimpleExceptionDemo {

  public void f() throws SimpleException {
    System.out.println("Throw SimpleException from f()");
    throw new SimpleException();
  }
  public static void main(String[] args) {
    SimpleExceptionDemo sed = new SimpleExceptionDemo();
    try {
      sed.f();
    } catch(SimpleException e) {
      System.err.println("Caught it!");
    }

  }
}

In some case i have this output:

在某些情况下,我有这个输出:

Caught it!
Throw SimpleException from f()

Do you know why "Throw SimpleException from f()" is printed after "caught it"?

你知道为什么“抓住它后”会打印出“从f()中抛出SimpleException”吗?

4 个解决方案

#1


8  

You are printing on two different output streams:

您正在两个不同的输出流上打印:

System.out.println("Throw SimpleException from f()");

and

System.err.println("Caught it!");

The order of the messages appearing from the two different streams is not guaranteed... Use the same stream, and it will be OK.

不保证从两个不同的流出现的消息的顺序...使用相同的流,它就可以了。

If you're interested in such issues, it might be interesting to read up on

如果您对此类问题感兴趣,请阅读它可能会很有趣

#2


7  

System.err and System.out are different streams, thus the one's output may appear before the other.

System.err和System.out是不同的流,因此一个的输出可能出现在另一个之前。

If you were to only use one, it would be in the correct order.

如果你只使用一个,那将是正确的顺序。

#3


2  

You are printing your exception to System.err stream. The rest of your output goes to System.out. These two are different system streams each with it's own buffers and timing, but, by default, with common receiving end - the console.

您正在将您的异常打印到System.err流。输出的其余部分转到System.out。这两个是不同的系统流,每个系统都有自己的缓冲区和时序,但默认情况下,使用公共接收端 - 控制台。

The thing is, that buffers in those two streams aren't synchronized, so there is no guarantee, that those two will output in your specified order.

问题是,这两个流中的缓冲区不同步,因此无法保证这两个流将以您指定的顺序输出。

You can force the streams to output immediately by calling flush() after the print.

您可以通过在打印后调用flush()来强制立即输出流。

#4


1  

because you are writing into two different streams - System.out and System.Err. Write both into same, then you will get in the order you wanted.

因为您正在写入两个不同的流--System.out和System.Err。将两者写入相同的内容,然后您将获得所需的顺序。

#1


8  

You are printing on two different output streams:

您正在两个不同的输出流上打印:

System.out.println("Throw SimpleException from f()");

and

System.err.println("Caught it!");

The order of the messages appearing from the two different streams is not guaranteed... Use the same stream, and it will be OK.

不保证从两个不同的流出现的消息的顺序...使用相同的流,它就可以了。

If you're interested in such issues, it might be interesting to read up on

如果您对此类问题感兴趣,请阅读它可能会很有趣

#2


7  

System.err and System.out are different streams, thus the one's output may appear before the other.

System.err和System.out是不同的流,因此一个的输出可能出现在另一个之前。

If you were to only use one, it would be in the correct order.

如果你只使用一个,那将是正确的顺序。

#3


2  

You are printing your exception to System.err stream. The rest of your output goes to System.out. These two are different system streams each with it's own buffers and timing, but, by default, with common receiving end - the console.

您正在将您的异常打印到System.err流。输出的其余部分转到System.out。这两个是不同的系统流,每个系统都有自己的缓冲区和时序,但默认情况下,使用公共接收端 - 控制台。

The thing is, that buffers in those two streams aren't synchronized, so there is no guarantee, that those two will output in your specified order.

问题是,这两个流中的缓冲区不同步,因此无法保证这两个流将以您指定的顺序输出。

You can force the streams to output immediately by calling flush() after the print.

您可以通过在打印后调用flush()来强制立即输出流。

#4


1  

because you are writing into two different streams - System.out and System.Err. Write both into same, then you will get in the order you wanted.

因为您正在写入两个不同的流--System.out和System.Err。将两者写入相同的内容,然后您将获得所需的顺序。