Is it bad practice to catch the Exception class instead of Exception sub-classes?

后端 未结 5 2073
夕颜
夕颜 2021-01-26 11:38

I have a method which throws subclasses of Exception. If I am performing the same catch functionality for each sub-class of Exception that\'s causght is it bad practice to just

5条回答
  •  逝去的感伤
    2021-01-26 12:05

    Yes, it's bad practice, because it makes the catch block catch runtime exceptions that shouldn't be caught.

    If you use Java 7, you can use

    catch (SomeException | SomeOtherException | YetAnotherException e) {
        ...
    }
    

提交回复
热议问题