Hardcore Java 评分人数不足
读书笔记 Chapter 5 : Exceptional Code
villim

when to make a class a subclass of Exception and when to make it a subclass of RuntimeException? Generally, this depends on the circumstances and the nature of the exception. If you consider NullPointerException, you know that this exception always indicates a programming error. However, with IllegalAccessException, the case is less clear. In some circumstances, such as during authorization control, it is a legitimate logic error. In other circumstances, such as in reflectionMethod( ), it indicates a programmer error. Although whether to make an exception a RuntimeException or an Exception is something of a judgment call, the following guidelines should help:

  • If an exception is always the result of a programming error, make it a direct descendant of RuntimeException.
  • If an exception represents a user's failure to enter data properly, you may be able to get away with using RuntimeException. This will allow you to propagate the RuntimeException in business logic classes without having to declare it. However, the GUI or other interface to the program should always watch for these kinds of errors. We will discuss one of these types of exceptions in Chapter 6.
  • If the exception is always the result of a business logic error, make it a direct descendant of Exception and use exception filtering when appropriate.

0
《Hardcore Java》的全部笔记 1篇
豆瓣
我们的精神角落
免费下载 iOS / Android 版客户端