我有一个问题,当抛出异常时,我的程序没有按预期结束。我已经追踪到 InputStream 和 BufferedInputStream 的组合吞下了异常。下面的代码演示了这个问题:
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
public class Test {
public static void main(String[] args) throws IOException {
try (InputStream stream = new BufferedInputStream(new MyStream())) {
int x = stream.read();
while (x != -1) {
System.out.println(x);
x = stream.read();
}
}
}
static class MyStream extends InputStream {
int i = 0;
@Override
public int read() throws IOException {
i = i + 1;
if (i == 5) {
throw new IOException("i == 5");
} …Run Code Online (Sandbox Code Playgroud)