如果我错了,请纠正我,但是,我相信try块中的语句首先被执行,然后,如果发生任何异常,finally块中的语句将被执行,然后catch块中的语句被执行.如果没有发生异常,则一旦try执行块中的语句并跳过catch块中的语句,就会执行finally块中的语句.
如果我的上述概念没有错,那么我不明白为什么这段代码不起作用:
// sock is an object of the class Socket
public void run() {
try {
in = sock.getInputStream();
scan = new Scanner(in);
out = sock.getOutputStream();
pw = new PrintWriter(out);
} catch (IOException e) {
e.printStackTrace();
} finally {
sock.close();
}
}
Run Code Online (Sandbox Code Playgroud)
它仍然说我需要在finally块中包含语句try-catch.
java ×1