Click here to Skip to main content
15,887,350 members
Articles / All Topics

Thinking in Easy TryCatch

Rate me:
Please Sign up or sign in to vote.
3.27/5 (5 votes)
6 Dec 2013CPOL 8.4K   1   9
How to think in easy trycatch

Introduction

These days, I'm concentrating on making my project stronger. I tried to add Try Catch everywhere I could. I encountered two main problems. The first is that some functions in the common library cannot use main project’s Log system, I finally found a solution for that. The second is that adding code Try {} + Catch(Exception){} takes me too much time, and it occupied lot of lines. I wanted to keep my code clear, so I made an easy trycatch by Func<T1, T2>. In my code, you can also use Extension to track your exception easily.

But unfortunately, my code is not very useful because of inefficiency of this method. Instead, use directly with Try Catch, my code costs 100 times more. And I found that my code is sometimes confusing for developers (who don’t use Action and Func). :(

C#
class Program
 {
 static void Main(string[] args)
 {
 Console.ReadLine();
 try
 {
 DataSet ds = new DataSet();
 Console.WriteLine(EasyTryCatch(ds));

 ds = null;
 Console.WriteLine(EasyTryCatchExt(ds));
 }
 catch (Exception exp)
 {
 Console.WriteLine(exp.Message);
 }
 Console.ReadLine();
 }

private static string EasyTryCatchExt(DataSet ds)
 {
 string functionName = "extension";
 return ds.Track((s) =>
 {
 if (s == null)
 throw new Exception("Yes " + functionName);
 else
 return true;
 }, "EasyTryCatchExt").ToString();
 }

private static string EasyTryCatch(DataSet ds)
 {
 return ExceptionFunction<DataSet, bool>.RunTrack(((s) =>
 {
 if (s == null)
 throw new Exception("Yes");
 else
 return true;
 }), ds, "EasyTryCatch").ToString();
 }

public static class ExceptionFunction<T1, T2>
 {
 public static T2 RunTrack(Func<T1, T2> func, T1 para, string functionName)
 {
 try
 {
 return func(para);
 }
 catch (Exception exp)
 {
 throw new Exception(functionName + " : " + exp.Message, exp);
 }
 }
 }
 }

public static class ExceptionFunc
 {
 public static TResut Track<T1, TResut>(this T1 para, Func<T1, TResut> func, string functionName)
 {
 try
 {
 return func(para);
 }
 catch (Exception exp)
 {
 throw new Exception(functionName + " : " + exp.Message, exp);
 }
 }
 }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
France France
A foolish 18 years old boy
Just got an Microsoft C# Specialist Certification. Objectif of this year : MCSD

Comments and Discussions

 
GeneralMy vote of 1 Pin
Richard Deeming10-Dec-13 7:55
mveRichard Deeming10-Dec-13 7:55 
GeneralRe: My vote of 1 Pin
comiscience10-Dec-13 8:15
comiscience10-Dec-13 8:15 
GeneralRe: My vote of 1 Pin
Richard Deeming10-Dec-13 8:33
mveRichard Deeming10-Dec-13 8:33 
GeneralRe: My vote of 1 Pin
comiscience11-Dec-13 0:05
comiscience11-Dec-13 0:05 
GeneralRe: My vote of 1 Pin
Richard Deeming11-Dec-13 2:23
mveRichard Deeming11-Dec-13 2:23 
GeneralRe: My vote of 1 Pin
comiscience13-Dec-13 0:05
comiscience13-Dec-13 0:05 
GeneralMy vote of 2 Pin
morzel9-Dec-13 6:04
morzel9-Dec-13 6:04 
GeneralRe: My vote of 2 Pin
comiscience9-Dec-13 6:34
comiscience9-Dec-13 6:34 
GeneralMy vote of 1 Pin
r.wcs8-Dec-13 23:26
r.wcs8-Dec-13 23:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.