Sticking to the question you asked, IllegalArgumentException
is a good exception.
However it doesn't look like you're using class hierarchy correctly. If both B
and C
extend A
which is abstract, then I think that the class A should have an abstract method returning whatever you're checking in your if
block chain:
public abstract class A {
public abstract String getHandling(); //<-- or whatever the correct name is
}
So that B
and C
can say this:
public final class B extends A {
@Override
public String getHandling() {
return "b";
}
}
public final class C extends A {
@Override
public String getHandling() {
return "c";
}
}
Then, your function that you show partially above would just declare someVar
being of type A
:
public void yourFunction(A someVar) {
return someVar.getHandling();
}
Like that, you never have to worry about implementing the new if
each time that someone extend A again.
Either your new class extends A
(and in that case it is obliged to provide the handling implementation), or simply the compiler won't accept the object in parameter for you.