简体   繁体   English

使用java中的BufferReader进行异常处理

[英]Exception handling with BufferReader in java

In this simple program, why do I have to handle an exception ? 在这个简单的程序中,为什么我必须处理异常?

public static void main (String args[ ]) throws IOException
   {
        BufferedReader in = new BufferedReader(
        new InputStreamReader(System.in));

    System.out.print("Insert something : ");
        System.out.println("You inserted : "+in.readLine());    
   }

BufferReader#readLine() method throws IOException so if you use this method you should either use it inside a try-catch block or add the throws IOException to the method in which you use it. BufferReader#readLine()方法抛出IOException因此如果您使用此方法,您应该在try-catch块中使用它,或者将throws IOException添加到您使用它的方法中。

Quoting from java doc 引自java doc

在此输入图像描述

And to learn more about the exception handling in general (of course in java) please read java doc . 要了解有关异常处理的更多信息(当然是在java中),请阅读java doc

Because of the contract of the close() method. 因为close()方法的契约。 From the JavaDoc: 来自JavaDoc:

Closes the stream and releases any system resources associated with it. 关闭流并释放与其关联的所有系统资源。 Once the stream has been closed, further read(), ready(), mark(), reset(), or skip() invocations will throw an IOException. 关闭流后,进一步的read(),ready(),mark(),reset()或skip()调用将抛出IOException。 Closing a previously closed stream has no effect. 关闭先前关闭的流无效。

When the buffered stream is closed, the BufferedReader can't read anything more and will notify the caller about this fact by throwing an IOException . 当缓冲流关闭时, BufferedReader不能再读取任何内容,并通过抛出IOException来通知调用者这一事实。

You need to handle IOException because it is a checked exception and method in.readLine() specifically declares it with a throws clause. 您需要处理IOException因为它是一个checked exceptionin.readLine()方法特别使用throws子句声明它。 That means IO Exception can be thrown at run time, so you should handle it. 这意味着可以在运行时抛出IO异常,因此您应该处理它。 Hope this helps. 希望这可以帮助。

Reading from stdin is very unlikely to cause any kind of IOException. 从stdin读取不太可能导致任何类型的IOException。 The BufferedReader doesn't know that, though, it assumes whatever it is wrapping will throw IOException and it is just passing it along. BufferedReader并不知道,但是假设它包装的任何东西都会抛出IOException而它只是传递它。 I would think about using the java.util.Scanner class instead, to avoid the annoyance of the BufferedReader checked exceptions here. 我会考虑使用java.util.Scanner类,以避免在这里烦扰BufferedReader检查的异常。

The JVM will handle closing stdin for you when the program terminates, so closing the reader shouldn't be an issue, except that you may end up needing to flush the BufferedReader to get all its contents. 当程序终止时,JVM将为您处理关闭stdin,因此关闭读取器应该不是问题,除了您可能最终需要刷新BufferedReader以获取其所有内容。 This is another reason to prefer Scanner. 这是偏爱Scanner的另一个原因。

For a small program it can be fine to let the main method throw exceptions, those will get written to the console (specifically, to stderr). 对于一个小程序,让main方法抛出异常就可以了,这些异常会被写入控制台(特别是stderr)。 As long as that is the behavior you want, throwing the IOException here is ok. 只要这是您想要的行为,抛出IOException就行了。 In real life a program could be executed from a shell script and the shell script could redirect the program's output to a file, so that anything thrown by the program would be logged. 在现实生活中,程序可以从shell脚本执行,shell脚本可以将程序的输出重定向到文件,以便记录程序抛出的任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM