Skip to content

Instantly share code, notes, and snippets.

@codeimpossible
Created March 18, 2014 05:57
Show Gist options
  • Save codeimpossible/9614280 to your computer and use it in GitHub Desktop.
Save codeimpossible/9614280 to your computer and use it in GitHub Desktop.
A really odd DSL around try/catch in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
namespace WithTrySample
{
public class WithTryResult<T>
{
public T Result { get; set; }
public Exception Error;
}
public static class WithTryResultExtensions
{
public static T GiveDefaultValueOnException<T> ( this WithTryResult<T> result )
{
return result.Error == null ? result.Result : default ( T );
}
public static TResult GiveDefaultValueOnException<T, TResult> ( this WithTryResult<T> result, TResult successValue )
{
return result.Error == null ? successValue : default ( TResult );
}
public static T IgnoreExceptions<T> ( this WithTryResult<T> result, params Exception[] exceptions )
{
if ( result.Error == null )
{
return result.Result;
}
if ( exceptions.Where ( e => e.GetType () == result.Error.GetType () ).Count () > 0 )
{
return default ( T );
}
else
{
throw result.Error;
}
}
public static WithTryResult<T> WhenExceptionCaught<T, EXCEPTION> ( this WithTryResult<T> result, Action<EXCEPTION> eaction )
where EXCEPTION : Exception
{
if ( result.Error is EXCEPTION )
{
eaction ( result.Error as EXCEPTION );
}
return result;
}
}
public static class ObjectExtensions
{
public static WithTryResult<RESULT> WithTry<TARGET, RESULT> ( this TARGET o, Expression<Func<TARGET, RESULT>> expression )
{
var compExp = expression.Compile ();
var result = new WithTryResult<RESULT> ();
try
{
result.Result = compExp ( o );
}
catch( Exception localException )
{
result.Error = localException;
}
return result;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
namespace WithTrySample
{
public class Worker
{
public int AddAllNumbers ( List<int> numbers )
{
if ( numbers == null )
{
throw new ArgumentNullException ( "numbers" );
}
if ( numbers.Count == 0 )
{
throw new ArgumentException ( "must have at least one item", "numbers" );
}
int total = 0;
numbers.ForEach ( i => total += i );
return total;
}
}
class Program
{
private static Worker _worker = new Worker ();
private static List<int> _emptyList = new List<int> ();
static void Main ( string[] args )
{
GiveDefaultValueOnException_ExampleOne ();
GiveDefaultValueOnException_ExampleTwo ();
IgnoreExceptions_Example ();
RethrowBehavior_Example ();
WhenExceptionCaught_Callback_Example ();
Console.ReadLine ();
}
public static void GiveDefaultValueOnException_ExampleOne ()
{
var result = _worker.WithTry ( w =>
w.AddAllNumbers ( null ) )
.GiveDefaultValueOnException ();
Console.WriteLine ( result.ToString () );
}
public static void GiveDefaultValueOnException_ExampleTwo ()
{
var tfResult = _worker.WithTry ( w =>
w.AddAllNumbers ( null ) )
.GiveDefaultValueOnException ( successValue: true );
Console.WriteLine ( tfResult.ToString () );
}
public static void IgnoreExceptions_Example ()
{
var result = _worker.WithTry ( w =>
w.AddAllNumbers ( _emptyList ) )
.IgnoreExceptions ( new ArgumentException () );
Console.WriteLine ( result.ToString () );
}
public static void RethrowBehavior_Example ()
{
try
{
var result = _worker.WithTry ( w =>
w.AddAllNumbers ( _emptyList ) )
.IgnoreExceptions ( new ArgumentNullException () );
}
catch ( ArgumentException )
{
Console.WriteLine ( "Exception Caught" );
}
}
public static void WhenExceptionCaught_Callback_Example ()
{
var result = _worker.WithTry ( w =>
w.AddAllNumbers ( _emptyList ) )
.WhenExceptionCaught<int, ArgumentException> ( e => Console.WriteLine ( e ) )
.GiveDefaultValueOnException ();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment