tag:blogger.com,1999:blog-4548789926995192649.post3295042728911262305..comments2024-03-11T10:37:42.281+00:00Comments on Paul Grenyer: Execute Around ExceptionPaul Grenyerhttp://www.blogger.com/profile/18212226926099615757noreply@blogger.comBlogger1125tag:blogger.com,1999:blog-4548789926995192649.post-61613431270648581992011-06-08T23:14:23.399+01:002011-06-08T23:14:23.399+01:00Like the idea of ExecuteSafely, although not convi...Like the idea of ExecuteSafely, although not convinced about the ExecuteAroundException, although that does allow it to be passed around as a sort of policy object.<br /><br />I've had a play around with this, and for my purposes come up with something like the stuff below:<br /><br /><br />// Provides primatves for further development<br />public static class XH<br />{<br /><br /> // Execute Safely<br /><br /> public static void ExecuteSafely< TException >(<br /> Action action,<br /> Action< TException > eh )<br /> where TException : Exception<br /> {<br /> try<br /> {<br /> action(null);<br /> }<br /> catch( TException ex )<br /> {<br /> eh( ex );<br /> }<br /> }<br /><br /> public static void ExecuteSafely(<br /> Action action )<br /> {<br /> Action< Exception > eh = NoOpExceptionHandler;<br /><br /> ExecuteSafely( action, eh );<br /> }<br /><br /><br /> // potentially could have versions taking multiple TException types for different actions, but becomes unwieldy very quickly.<br /> // e.g. to perform logging on all exceptions to a DB, but do something more clever on a DBGoneAwayException. <br /> public static TReturnType ExecuteSafely< TReturnType, TException >(<br /> Func action,<br /> Action< TException > eh )<br /> where TException : Exception<br /> {<br /> try<br /> {<br /> return action(null);<br /> }<br /> catch( TException ex )<br /> {<br /> eh( ex );<br /><br /> return default(TReturnType);<br /> }<br /> }<br /><br /><br /> public static TReturnType ExecuteSafely(<br /> Func action )<br /> {<br /> Action< Exception > eh = NoOpExceptionHandler;<br /><br /> return ExecuteSafely( action, eh );<br /> }<br /><br /><br /> // Execute <br /><br /> public static void Execute< TException >(<br /> Action action,<br /> Func< TException > eh )<br /> where TException : Exception<br /> {<br /> try<br /> {<br /> action(null);<br /> }<br /> catch( TException ex )<br /> {<br /> if( eh( ex ) )<br /> {<br /> throw;<br /> }<br /> }<br /> }<br /><br /> public static void Execute(<br /> Action action )<br /> {<br /> Func< Exception > eh = delegate( Exception ex)<br /> {<br /> NoOpExceptionHandler( ex );<br /> return false;<br /> }<br /><br /> Execute( action, eh );<br /> }<br /><br /><br /> // potentially could have versions taking multiple TException types for different actions, but becomes unwieldy very quickly.<br /> // e.g. to perform logging on all exceptions to a DB, but do something more clever on a DBGoneAwayException. <br /> public static TReturnType ExecuteSafely< TReturnType, TException >(<br /> Func action,<br /> Func< TException > eh )<br /> where TException : Exception<br /> {<br /> try<br /> {<br /> return action(null);<br /> }<br /> catch( TException ex )<br /> {<br /> if( eh( ex ) )<br /> {<br /> throw;<br /> }<br /> }<br /> }<br /><br /><br /> public static TReturnType ExecuteSafely(<br /> Func action )<br /> {<br /> Func< Exception > eh = delegate( Exception ex)<br /> {<br /> NoOpExceptionHandler( ex );<br /> return false;<br /> }<br /><br /> return Execute( action, eh );<br /> }<br /><br /><br /> // Common Exception handling Policies<br /> public static void NoOpExceptionHandler( Exception ex )<br /> {<br /> }<br />}<br /><br /><br />Where I've used this I've provided local ExecuteSafely in the class that knows how to handle the exception:<br /><br />class AboutBox : Form<br />{<br /> private btnExplode_Click( ... )<br /> {<br /> ExecuteSafely( delegate {<br /> throw new Exception();<br /> });<br /> }<br /><br /><br /> public static void ExecuteSafely(<br /> Action action )<br /> {<br /> Action< Exception > eh = delegate( Exception ex ) {<br /> MessageBox.Show( this, "It's broken" );<br /> };<br /><br /> XH.ExecuteSafely( action, eh );<br /> }<br /><br />}Anonymoushttps://www.blogger.com/profile/18317243801151759117noreply@blogger.com