I read one piece of codes:
public void myfunc() throws MyException {
try {
// codes here
} catch(AlreadyExistsException e) {
throw new sub1MyException("yyy"); // first catch block
} catch(Exception e) {
throw new sub2MyException("xxx"); // second catch block
}
}
sub1MyException and sub2MyException are both sub-classes of MyException. AlreadyExistsException is NOT sub-class of MyException.
I have some questons:
1, will throw new sub1MyException("yyy"); be caught by the second catch block?
2, Any anti-patterns in the above codes. I did some search but did not find any similar patterns or examples. If any, please leave a comment.
3, I think, it is not necessary to use so many sub-classes because exception messages already in the Exception. Custom messages such as yyy and xxx not very helpful.
Thanks
new SomeException("error 1")and for something elsenew SomeException("error 2")is most of the time a bad idea