java.lang.Exception vs. rolling your own exception

后端 未结 10 1202
无人及你
无人及你 2020-12-13 14:30

At what point would you create your own exception class vs. using java.lang.Exception? (All the time? Only if it will be used outside the package? Only if it must contain ad

10条回答
  •  我在风中等你
    2020-12-13 15:14

    In most cases it doesn't make sense to create your own exception class.

    There is a tendency in novice programmers to create their own exception class just so they can use a name that is more indicative of the type of error. So you'll find classes like FTPInitializationException, DAOFactoryException etc. even though such exceptions are not being handled differently than standard exceptions. This is clearly an anti pattern that should be avoided.

提交回复
热议问题