Java, Class-specific Exceptions vs. Standard Exceptions

前端 未结 6 1813
北恋
北恋 2021-02-07 12:42

I\'ve been updating an existing library to throw exceptions to help improve debugging by people using the library.

At first, I thought I\'d define exceptions specific to

6条回答
  •  情深已故
    2021-02-07 13:28

    I'd use explicitly defined exceptions to give the client code more control. If the client wants to, they can catch IllegalArgumentException as in your example above. If they need more control, they can catch individual types of exceptions. Consider, for example, a method that could throw two subclasses of IllegalArgumentException. If you don't subclass, you have to do string parsing or some other nonsense to determine the actual cause of the thrown exception. User-defined types solves this issue.

提交回复
热议问题