1

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
  • Can't you just add a throws-declaration to your main method? (I don't have Eclipse installed on this computer, so I can't test it myself, unfortunately...) – Tomas Aschan Dec 26 '12 at 03:35
  • Not when the call hierarchy is really deep. At the point of this exception, I'd have to add over 13 throws declarations, which isn't something I want to do. – Qix - MONICA WAS MISTREATED Dec 26 '12 at 03:36
  • Check out [this answer](http://stackoverflow.com/a/3624776/38055) - the options pane shown there seems to have a lot of options for various errors and warnings. Maybe there's something there? – Tomas Aschan Dec 26 '12 at 03:38
  • Sounds like it's a checked exception, which you must either surround with a try/catch or add in the throws clause. Since, it is a syntax error you cannot suppress it. What you can do is catch it and throw a new unchecked exception, which doesn't require that throws clause. – Bhesh Gurung Dec 26 '12 at 03:40
  • `UncheckedExceptions`, as far as I know, are useless with `throw` because they don't actually do anything (they're mainly for documentation purposes). `RuntimeException` is what I wanted. – Qix - MONICA WAS MISTREATED Dec 26 '12 at 03:49
  • 1
    @Qix: Errors and runtime exceptions are collectively known as unchecked exceptions. Please have a read of the article linked to by yshavit. – Greg Kopff Dec 26 '12 at 03:56
  • @GregKopff, Ah my mistake. I read another SO question wrong. – Qix - MONICA WAS MISTREATED Dec 26 '12 at 04:28

1 Answers1

4

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.

yshavit
  • 42,327
  • 7
  • 87
  • 124