public static void Main()
Trier<string> str = "someString";
var result = str.Try(param => param.Replace("someString", "REPLACED"))
.Catch((x, exception) => Console.WriteLine("An Error Occured", exception))
Console.WriteLine(result);
public class ExecutableTryCatch<T> {
private Func<T, T> tryAction;
private Action<T, Exception> catchAction;
public ExecutableTryCatch(T content, Func<T, T> tryAction, Action<T, Exception> catchAction) {
this.tryAction = tryAction;
this.catchAction = catchAction;
this.Content = this.tryAction(this.Content);
catch(Exception exception) {
if(this.catchAction == null)
this.catchAction(this.Content, exception);
private Trier(T content) {
public Catcher<T> Try(Func<T, T> tryAction) {
return new Catcher<T>(this.content, tryAction);
public static implicit operator Trier<T>(T content) {
return new Trier<T>(content);
public class Catcher<T> {
private Func<T, T> tryAction;
public Catcher(T content, Func<T, T> tryAction) {
this.tryAction = tryAction;
public ExecutableTryCatch<T> Catch(Action<T, Exception> catchAction) {
return new ExecutableTryCatch<T>(content, tryAction, catchAction);