I have two classes inheriting from java.lang.Exception
. They both have a method with the same signature void a(){...}
. They both can be thrown in a code block. If I do:
catch (SubException1 | SubException2 e)
{
e.a();
}
Then it won't compile because method a()
does not belong to Exception. Is it a Java language flaw? How should I design my code properly to prevent code redundancy?
a
defined in some common superclass ofSubException1
andSubException2
? - luk2302void a()
. Then you can catch the ancestor and calle.a()
- IshnarkException
in your case. So it doesn't really matter thatSubException1
hasa()
. You can solve this by having a common supertype witha()
on both methods, as already was suggested. - M. Prokhorov