posted 19 years ago
My exception hierarchy is as follows: custom exception class
MyException extending Exception. and MySQLException extending
MyException.
Exception->MyException->MySQLException
Now consider following case:
If there is a method m1 that has scope in which calls to two methods
m2 and m3 are made. m2 throws MyException and m2 throws
MySQLException.
Now how should the throws clause look like ?
Should it be
1) m1() throws MyException, MySQLException or
2) m1 throws MyException
My choice in (2).
In short: when there are parent and child exceptions thrown in
implementation should we go with approach (2) always throw parent ?
Now another point is if there is a method m4() that calls m1(). Just
looking at the signature of m1() it seems that MyException is thrown
here. But how will the caller client would know that m1() throws
MySQLException in some condition and for other condition throws
MyException. If we go with approach (1), from the signature itself the
caller would know that there can be 2 separate exceptions that may be
thrown from method m1(). This can be a justification for approach(1) but I personally think javadoc with approach(2) is the way to go.
What do you think ?