I have the following situation.
I have a Java Class that inherits from another base class and overrides a method.
The base method does not throw exceptions and thus has no throws ... declaration.
Now my own method should be able to throw exception but I have the choices to either
- Swallow the exception or
- Add a throws declaration
Both a not satisfying because the first one would silently ignore the exception (ok I could perform some logging) and the second would generate compiler errors because of the different method headers.
public class ChildClass extends BaseClass {
@Override
public void SomeMethod() {
throw new Exception("Something went wrong");
}
}
You can throw unchecked exceptions without having to declare them if you really want to. Unchecked exceptions extend
RuntimeException. Throwables that extendErrorare also unchecked, but should only be used for completely un-handleable issues (such as invalid bytecode or out of memory).As a specific case, Java 8 added
UncheckedIOExceptionfor wrapping and rethrowingIOException.