java异常保留封装示例
我有一个类管理磁盘上的文件,以 xml 的形式保存报告。 由于它使用 JAXB(但可以使用任何其他 xml 库,它不相关),因此它可以生成 jaxb(或任何其他 xml 库)检查异常。
为了保留封装性,我应该将原始库异常“转换”为库逻辑级别的内容。但我不想增加太多相关性较差的课程的数量。 由于该类与文件相关,因此我认为 IOException 足以满足这种情况。
public void save(File file) throws IOException {
try{
JAXBContext jc = JAXBContext.newInstance(ArchiveInfo.class);
Marshaller m = jc.createMarshaller();
m.marshal(this, file);
} catch (JAXBException jexc) {
throw new IOException(jexc);
}
}
- 您是否同意这个解决方案,是否是简单性和正确性之间的良好权衡?
- 我很幸运,因为我找到了一个合适的例外。如果不是,我必须定义自己的异常,因为使用 Exception 类并不是一个好的设计。 (导致调用者捕获 Exception,同时也隐式捕获 RuntimeException)。 运行时异常在 RuntimeException 类中有一个共同的祖先,而所有检查的异常却没有,这难道不是异常结构中的一个异常吗?
I have a class that manages a file on disk, saving a reoport in the form of xml.
Since it uses JAXB (but could use any other xml library, it's not relevant) it could generate jaxb (or anyother xml library) checked exceptions.
In order to preserve encapsulation, I should "convert" my original-library exception in something at the logical level of my library. But I not want to increase much the number of classes with little, poorly relevant classes.
Since the class is file-related, i think that IOException is adequate for the case.
public void save(File file) throws IOException {
try{
JAXBContext jc = JAXBContext.newInstance(ArchiveInfo.class);
Marshaller m = jc.createMarshaller();
m.marshal(this, file);
} catch (JAXBException jexc) {
throw new IOException(jexc);
}
}
- Do you agree with this solution, is a good trade-off between simplicity and correctness?
- I've been lucky, because i found a suitable exception. If I wasn't, I would necessarily define my own exception, because using Exception class isn't good desing. (Causes the caller to catch Exception, catching implicitly also RuntimeException).
Isn't it an anomaly in the structure of exceptions that runtime exceptions have a common ancestor in RuntimeException class, while all checked exceptions haven't?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1)我同意你的观点,并且倾向于这样做:手动保留大量异常类型在大多数情况下是无用的。在应用程序的较高级别,您所需要的只是足够的精度,以便能够向用户提供有意义的错误。在您的情况下“无法读取文件”,因此 IOException 是可以的。
2)是的,这是Java中异常机制的一个长期记录的缺陷...有些人甚至会说Java中的异常机制从根本上来说是有缺陷的(检查异常大多数时候都被滥用)。无论如何,在你的情况下,我有时会使用万能的 InvalidStateException 或 IllegalArgumentException。当然,如果您需要将更精确的含义传达给应用程序的上层,您始终可以创建自己的例外。但请记住:亲吻:)
1) I agree with you and tend to do the same: manually keeping lots of exception types is useless most of the time. All you need at the higher levels of the app is sufficient precision to be able to provide a meaningful error to the user. In your case "File could not be read", hence IOException is OK.
2) Yes it is a long-documented flaw of the Exception mechanism in Java... Some would even say the Exceptions mechanism in Java is flawed at its root (checked exceptions are misused most of the time). Anyway in your case, I sometimes use the catch-it-all InvalidStateException or IllegalArgumentException. Of course you can always create your own exceptions if you need more precise meaning to be conveyed to upper layers of your app. But remember : KISS :)