展开全部
因为有些代码在编62616964757a686964616fe78988e69d8331333337386661译的时候就会检测会不会有明显的,也就是eclipse预定义的一些exception
所以需要你给抛出异常,你如果写外面是不会抛出这些异常的,所以编译时会报错,不过你如果不想写try里面,你可以在方法上写Throws Exception,
这样就可以不写try块
如果你非要加,可以如下这样加,但没什么意义try (
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
) {
try {
String inputLine, outputLine;
KnockKnockProtocol kkp = new KnockKnockProtocol();
outputLine = kkp.processInput(null);
out.println(outputLine);
while ((inputLine = in.readLine()) != null) {
outputLine = kkp.processInput(inputLine);
out.println(outputLine);
if (outputLine.equals("Bye"))
break;
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}