public class try
{
public static void main(String[] args)
{
try
{
if(true)
throw new A();
if(true)
throw new B();
}
catch( A | B e)
{
e.doit();
}
}
}
class A extends Exception
{
public void doit() {}
}
class B extends Exception
{
public void doit() {}
}
This doesn't compile
18: error: cannot find symbol
e.doit();
^
symbol: method doit()
location: variable e of type Exception
The variable e
seems to end up as type Exception
rather than the actual type - this seems logical because at compile type the compiler doesn't know what kind is going to be thrown. However, is there a way to make this work without making A
& B
both derive from some common base class or implement a common interface?