您的异常应该命名为 PrinterException 或 PrintingException
给定一个接口,比如
Printer {
print()
}
它应该抛出 PrinterException 还是 PrintingException。我猜 PrinterException 更通用,因为它也可以用于打印机执行的但实际上没有打印的方法,例如 shutdown() 之类的方法。
如果打印机实际上有两种方法
Printer {
shutdown() throws PrinterShuttingDownException;
print() throws PrintingException
}
或者
Printer {
shutdown() throws PrinterException;
print() throws PrinterException;
}
Given an interface like
Printer {
print()
}
should it throw PrinterException or PrintingException. I guess PrinterException is more generic in that it can also be used for methods done by the Printer that is not actually printing, say something like turnoff().
What if Printer actually had two methods
Printer {
shutdown() throws PrinterShuttingDownException;
print() throws PrintingException
}
or
Printer {
shutdown() throws PrinterException;
print() throws PrinterException;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我总是会遇到 PrinterException。这是一个异常,由打印机抛出。
I would always have PrinterException. It's an Exception, thrown by the Printer.
我认为 PrinterException 更好,因为它定义了不一定与打印行为相关的异常,如果参数不正确或者由于一些不相关的问题(例如记录到文件的问题)而失败,则可能会发生这种情况。
不过,它是主观的,所以这取决于你。
编辑:
如果您有两个操作:打印和关闭,则打印应抛出 PrintingException,关闭应抛出 PrinterShutdownException,这两个操作都源自 PrinterException。这将使您能够捕获特定情况或与打印机相关的任何异常。
I would argue PrinterException is better because it defines exceptions which aren't necessarily related to the act of printing, which is possible if the arguments aren't correct or it fails for a somewhat unrelated issue like problems logging to a file.
Though, it's subjective so it's up to you.
Edit:
If you have two operations, print and shutdown, then print should throw a PrintingException and shutdown should throw a PrinterShutdownException, both of which would derive from PrinterException. This would allow you to catch specific cases or any exception related to the printer.