Pergunta

I'm extending the class RecordingCommand from org.eclipse.emf.transaction.RecordingCommand; I need to override protected method doExecute(), the method definition does not contain any exception to throw but there is option that inside my code I have and exception and I want to catch it and raise it up, how should I handle this kind of exception, here I throw the message e but I'm not sure that this is the right way to do that.

I hope you will understand the issue since I think it more sophisticated that just throw E

i.e.throw the exception

@Override 
protected void doExecute() {
    try { 
        //my logic
   } catch(Exception e) {
        throw e;
   }
}
Foi útil?

Solução

You can always throw an UncheckedException from a method, which need not be declared in the throws clause.

So, you wrap your exception in any unchecked exception like that extends RuntimeException or even RuntimeException itself.

try {
    //my logic
} catch(Exception e) {
     throw new RuntimeException(e);
}

And just for the sake of completeness, it is a bad idea to have a single catch block for Exception, which will catch all the exceptions. Rather you should have catch block for specific type of exception your method may throw.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top