Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Well, the easiest and straight forward solution would be tu use plain java try catchs, not that flexible, but often useful enough as error handling strategies does not change that often. Those exceptions you can not catch are declared on the method to may be thrown, and may be handled by your Object B.</p> <p>If you wanna be more flexible. Create an interface for your service A with all the possible exceptions declared. Implement that interface with logic but without any error handling. Then you could create ErrorStrategy objects that implement the interface as well and delegate incoming calls to another implementation of that interface, BUT and this is the interesting part append some error handling strategy for one or more particular exceptions. Here is an example to make it more understandable.</p> <pre><code>public interface A { void someMethod() throws IOException, MalformedURLException; } class AImpl implements A { @Override public void someMethod() throws IOException, MalformedURLException { // here goes your business logic } } class ErrorHandlerOne implements A { @Override public void someMethod() throws IOException { try { delegate.someMethod(); } catch (MalformedURLException e) { // handle the exception } } } </code></pre> <p>If you wanna be even more flexible, I would recommend to use AOP mechanisms instead of the simple delegation chain, that way you can easily plug-in and exchange your error handling strategies. If you use Spring and your Service A is a Spring-Bean you could easily use Springs build in AOP support. In that case the after throwing advice is what you looking for.</p> <p>After throwing advice: Advice to be executed if a method exits by throwing an exception.</p> <pre><code>import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.AfterThrowing; @Aspect public class AfterThrowingExample { @AfterThrowing( pointcut="com.xyz.myapp.A.someOperation()" throwing="ex") public void doRecoveryActions(IOException ex) { // ... } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload