Wrap exceptions by runtime exceptions with an annotation

ripper234 picture ripper234 · Jun 27, 2009 · Viewed 9.3k times · Source

Is there a way to annotate a method so all exceptions thrown are converted to runtime exception automagically?

@MagicAnnotation
// no throws clause!
void foo()
{
  throw new Exception("bar")'
}

Answer

sinuhepop picture sinuhepop · Nov 30, 2012

Project Lombok's @SneakyThrows is probably what you are looking for. Is not really wrapping your exception (because it can be a problem in a lot of cases), it just doesn't throw an error during compilation.

@SneakyThrows
void foo() {
    throw new Exception("bar")'
}