Catching an exception that is nested into another exception

后端 未结 7 1589
暗喜
暗喜 2020-12-05 10:18

I want to catch an exception, that is nested into another exception. I\'m doing it currently this way:

} catch (RemoteAccessException e) {
    if (e != null          


        
7条回答
  •  时光取名叫无心
    2020-12-05 10:31

    I see no reason why you want exception handling to be efficient and elegant, I settle for effective. They're called Exceptions for a reason.

    This code will be a maintenance nightmare. Can't you redesign the call stack to throw the Exception you are interested in? If it is important the method signatures should show it and not hide it wrapped in 2 other exceptions.

    The first (e != null) is unnecessary.

    And you can change the 3rd better to e.getCause().getCause() instanceof MyException)

提交回复
热议问题