Is there a way to get Eclipse to suppress errors for a missing throws declaration when throwing exceptions within methods?

If you are to build and run something without explicitly specifying throws, it will unwind to the main method and crash the program (which is what I want in this particular case). There are certain exceptions that do it, such as sun.reflect.generics.reflectiveObjects.NotImplementedException, which allows you to throw it without needing to specify throws.

有帮助吗?

解决方案

Eclipse isn't doing anything special here; the issue is in the Java code itself. Your two best options are to change the method's signature to include the throws, or to catch the exception and rethrow it as a RuntimeException (or subclass of RuntimeException).

try {
    doWhatever();
} catch (SomeException e) {
    throw new SomeRuntimeException(e);
}

...where SomeException is or extends Exception (but not RuntimeException), and SomeRuntimeException is or extends RuntimeException.

For more info, check out the section in Sun's Java tutorials on checked Exceptions.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top