I'm trying to override a method, with throwing an exception:
class A {
public doSomething(){
// some of logic
}
}
class B extends A {
public doSomething() throws MyCustomizedException {
try {
// some of logic
} catch(ExceptionX ex ) {
throw new MyCustomizedException(" Some info ", ex);
}
}
}
But I get this compile time error :
Exception MyCustomizedException is not compatible with throws clause in A
The two constraints are :
How can I get rid of the exception?
Thank you a lot.
Cannot be done.
When you override a method, you can't break the original contract and decide to throw a checked exception.
You can make MyCustomizedException unchecked. You can throw it, but you can't require that users handle it the way you can with a checked exception. The best you can do is add it to the javadocs and explain.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With