If a class method from package A throws its own exception defined in package A , and the class is used in package B , is it possible to make an exception class package private ? IDEA shows what you can. But then I can not catch him in B obtained?
|
1 answer
It will not work to catch it in catch, because the code just won't compile
catch (MyPrivateException e) // error It will be possible to catch it with a more general type.
catch (Exception e) // MyPrivateException поймается But if someone wants in the handler to write different logic for different exceptions, problems arise, so it’s better not to do so.
catch (Exception e) { if (e.getClass().getName().contains("MyPrivateException") { // единственный способ отличить такое исключение handlePrivateException(e); } else { throw e; } } |
RuntimeException, and if I don’t have to catch it? - insider Nov.