简体   繁体   English

我应该如何处理BufferReader.readline()函数引发的IOException?

[英]How should I handle the IOException thrown by the function BufferReader.readline()?

Everytime I encounter a java exception ,I always fell into the dilemma whether I should throw the exception directly or catch the exception .If I catch the exception ,should I just print the stack trace or do more work in the catch block?For example , I am a reading a file line by line,the function BufferedReader.readLine() throws an IOException ,it seems that it is an checked (compared with unchecked exception) Exception because user was told to deal with this exception explicitly,right?Although I have to deal with it , but it seems that I can do nothing else but print stack trace, well ,it is really strange.Should I catch or throw this exception? 每次遇到Java异常时,我总是陷入困境,是直接抛出异常还是捕获异常。如果捕获异常,我应该打印堆栈跟踪还是在catch块中做更多的工作?例如,我正在逐行读取文件,函数BufferedReader.readLine()抛出IOException ,这似乎是一个已检查 (与未检查的异常相比)异常,因为有人告诉用户显式处理此异常,对吗?必须处理它,但看来我只能打印堆栈跟踪,别无选择,这确实很奇怪。我应该捕获还是抛出此异常? If I catch it,what should I do in catch block? 如果我接住了,我应该怎么做?

Its not about only printing the stacktrace, 它不仅要打印stacktrace,

  • You can put the exception info in separate log 您可以将异常信息放在单独的日志中
  • In case of exception you can close and delete the file if you are writing or whatever you want. 如果发生异常,则可以在写入或任何所需的内容时关闭并删除该文件。

If you duck the exception, the calling method will have to handle it. 如果避开该异常,则调用方法将必须处理该异常。

You can do the following things: 您可以执行以下操作:

  1. Log the Exception information on a log file. 将异常信息记录在日志文件中。 You can use the following method to populate the Exception information. 您可以使用以下方法来填充“异常”信息。

    public String populateExceptionStackTrace(Exception e) { StringBuilder sb = new StringBuilder(); public String populateExceptionStackTrace(Exception e){StringBuilder sb = new StringBuilder(); sb.append(e.toString()).append('\\n'); sb.append(e.toString())。append('\\ n'); for (StackTraceElement elem : e.getStackTrace()) { sb.append("\\tat ").append(elem).append('\\n'); 对于(StackTraceElement elem:e.getStackTrace()){sb.append(“ \\ tat”).append(elem).append('\\ n'); } return sb.toString(); } return sb.toString(); } }

  2. Try to close the InputStream if it is not null. 如果InputStream不为null,请尝试关闭它。

  3. Throw an Exception of your own with meaningful message to let user know what happens. 使用有意义的消息抛出您自己的异常,以使用户知道会发生什么。

This is about which part is responsible for this Exception . 这是关于哪个部分负责此Exception

If, for example, you're developing a tool package as a jar, maybe you should just throw the IOException in a method like PrasadIOUtils.ReadFile(String path) , which should just provide the function to read a file, not to handle the exception. 例如,如果您正在以jar形式开发工具包,也许您应该将IOException扔进类似PrasadIOUtils.ReadFile(String path)的方法中,该方法只应提供读取文件的功能,而不是处理例外。

In contrast, you should catch the Exception when you invoking method PrasadIOUtils.ReadFile(String path) in your code. 相反,在代码中调用方法PrasadIOUtils.ReadFile(String path)时,应捕获Exception Because this is your logic part, and you are the one who is responsible for handling the Exception to make your own project more robust and fault-tolerant. 因为这是您的逻辑部分,并且您是负责处理Exception以使您自己的项目更加健壮和容错的人。

This is just How I understand, hope it helps. 这只是我的理解,希望对您有所帮助。 :) :)

PS If you're still confused about there's nothing to do but printing the expection's stack trace, you can google exception chaining for further information. PS:如果您仍然对打印期望的堆栈跟踪信息无所适从,则可以在Google 异常链接中获取更多信息。 This is a java design pattern, maybe it'll make you understand more on Exception . 这是一种Java设计模式,也许会让您对Exception了解更多。

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

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