为PrintWriter和PrintStream的从未引发IOException [英] PrintWriter and PrintStream never throw IOExceptions

查看:183
本文介绍了为PrintWriter和PrintStream的从未引发IOException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

若干天前,我意识到, PrintWriter的(以及的PrintStream 从未抛出IOException 当写作,冲洗或关闭。

Some days ago I realized that PrintWriter (as well as PrintStream) never throw an IOException when writing, flushing or closing.

相反,它设置了一个内部​​标志(麻烦= TRUE )时发生错误。

这是不可能得到确切的例外,但只有如果有一些例外(<一个href=\"http://java.sun.com/javase/6/docs/api/java/io/PrintWriter.html#checkError()\">checkError()).

Instead it sets an internal flag (trouble=true) when an error occurs.
It's not possible to get the exact exception, but only if there was some exception (checkError()).

我的问题是:为什么有人希望能有这样的行为?并不坏API设计?

My question is: why would one want to have such behavior? Isn't that bad API design?

推荐答案

我觉得,既然的System.out System.err的的PrintStream ,提供了一些更轻松的错误处理。这可能是,因为其他海报提及,顺利为那些从C / C ++大约1995年,当添加了读/写API,的PrintWriter 被创造过渡的方式平行现有的的PrintStream

I think that since System.out and System.err are instances of PrintStream, some more relaxed error handling was provided. This was probably, as other posters have mentioned, to smooth the way for those transitioning from C/C++ circa 1995. When the Reader/Writer API was added, PrintWriter was created to parallel the existing PrintStream.

一个应用程序,其中这种行为是非常可取的记录。日志是附属于一个更大的应用程序。通常情况下,如果日志记录失败,人们不希望整个应用程序失败。因此,它是有道理的 System.err的,至少忽略例外。

One application where this behavior is extremely desirable is logging. Logging is ancillary to a larger application. Typically, if logging fails, one doesn't want that the entire application to fail. Thus, it makes sense for System.err, at least, to ignore exceptions.

这篇关于为PrintWriter和PrintStream的从未引发IOException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆