您的异常应该命名为 PrinterException 或 PrintingException

发布于 2024-10-15 22:45:55 字数 458 浏览 2 评论 0原文

给定一个接口,比如

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

同尘 2024-10-22 22:45:56

我总是会遇到 PrinterException。这是一个异常,由打印机抛出。

I would always have PrinterException. It's an Exception, thrown by the Printer.

心凉怎暖 2024-10-22 22:45:55

我认为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文