Click here to Skip to main content
15,896,207 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.5K   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 
Have a look at this[^], particularly step 2:

Do not catch exceptions in your code unless you can do one of the following three good things:
  • You can add more detail
  • You can fix the problem
  • You're the application root

All your code is doing is adding a string to the start of the exception message. You've set it up to be the name of the function, which would already be part of the stack-trace. However, the compiler will not enforce this, and it can quickly get out of sync with the real function name.

By catching every type of exception, you are potentially catching things that you can't handle. For example, an OutOfMemory exception means that you're unlikely to be able to create a new Exception object to throw.

By wrapping every exception in the base Exception type, you make it virtually impossible for code at a higher level to do any useful error handling. It has to resort to catching the Exception and checking the type of the InnerException, potentially down a long list of calls which used your method.

The vast majority of methods should contain no error handling at all. Let the exceptions propagate up to code which knows how to handle them. If nothing knows how to handle them, then your application should not attempt to continue, as it could end up corrupting data.



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


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.