Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Exception Handling via Strategy Pattern?
    primarykey
    data
    text
    <p>I cooked up a class <code>ExceptionHandler&lt;T extends Exception, OptionalReturnType&gt;</code> (see below) to eliminate some (what I view as) boilerplate code which was cluttering up actual implementation, while still providing a hook for explicit Exception handling if desired in the future. For the most part, in my application (essential a scientific computation), there is no such thing as recovery from exceptions - I need a log of the problem so I can fix it, but otherwise I'm just going to re-run once the problem is corrected.</p> <p>Do other people do this (at least, in my specific application situation)? Is it dumb to do so (if yes, some explanation as to why would be nice)?</p> <p>ExceptionHandler:</p> <pre><code>public abstract class ExceptionHandler&lt;ExceptionType extends Exception,OptionalReturn&gt; { public abstract OptionalReturn handle(ExceptionType e); //assorted boilerplate ExceptionHandling, e.g.: public static &lt;ET extends Exception&gt; ExceptionHandler&lt;ET, ?&gt; swallower(final boolean printStackTrace, final String string) { return new ExceptionHandler&lt;ET,Object&gt;() { @Override public Object handle(ET e) { if(printStackTrace) { e.printStackTrace(); } if(string!=null &amp;&amp; !string.isEmpty()) { System.err.println(string); } return null; } }; } public static &lt;ET extends Exception&gt; ExceptionHandler&lt;ET, ?&gt; swallower() { return swallower(false,null); } } </code></pre> <p>example use (which I'm in the process of chopping down so I'm actually not writing quite so much):</p> <pre><code>public class Getter&lt;From&gt; implements Function&lt;Future&lt;? extends From&gt;, From&gt; { private ExceptionHandler&lt;InterruptedException,?&gt; IEH; private ExceptionHandler&lt;ExecutionException,?&gt; EEH; public static final ExceptionHandler&lt;InterruptedException,?&gt; IEH_SWALLOWER = ExceptionHandler.swallower(true,"Returning null."); public static final ExceptionHandler&lt;ExecutionException,?&gt; EEH_SWALLOWER = ExceptionHandler.swallower(true,"Returning null."); private Getter() { this(IEH_SWALLOWER,EEH_SWALLOWER); } private Getter(ExceptionHandler&lt;InterruptedException,?&gt; IEH, ExceptionHandler&lt;ExecutionException,?&gt; EEH) { this.IEH = IEH; this.EEH = EEH; } public static &lt;T&gt; Getter&lt;T&gt; make() { return new Getter&lt;T&gt;(); } public static &lt;T&gt; Getter&lt;T&gt; make(ExceptionHandler&lt;InterruptedException,?&gt; IEH, ExceptionHandler&lt;ExecutionException,?&gt; EEH) { return new Getter&lt;T&gt;(IEH, EEH); } @Override public From apply(Future&lt;? extends From&gt; from) { if (from==null) throw new NullPointerException("Null argument in call with Getter."); return getter(from, IEH, EEH); } private static &lt;T&gt; T getter(Future&lt;T&gt; src, ExceptionHandler&lt;InterruptedException,?&gt; IEH, ExceptionHandler&lt;ExecutionException,?&gt; EEH) { try { return src.get(); } catch (InterruptedException e) { IEH.handle(e); } catch (ExecutionException e) { EEH.handle(e); } return null; } } </code></pre> <p>which is used with the <a href="http://code.google.com/p/guava-libraries/" rel="nofollow noreferrer">Guava</a> libraries to do some embarrassingly-parallel calculations, and makes the actual Iterable transformation of Futures into something like <code>Iterables.transform(futureCollection,Getter.make())</code> instead of tangle of inner-classes and exception handling.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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