Throwing generic Exception discouraged?

前端 未结 5 554
面向向阳花
面向向阳花 2021-01-04 04:04

Why is it discouraged to throw a generic (java.lang.Exception) exception, when it is usually sufficient to handle most conditional failures within a method? I understand tha

5条回答
  •  温柔的废话
    2021-01-04 04:31

    As always: It depends.

    I think there is a difference between API that you expose to others. That may live for some time. In that case you don't know what the caller considers best for his case.

    On the other hand there always is code that you use internally only for yourself. Than it might be sufficient to throw an generic exception. But remember, that you might want to change some exception handling later on. That will be harder when all error cases are mangled together.

提交回复
热议问题