Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# try catch pattern help

We always need to try catch in our code and it becomes ugly like

public void foo()
{
  try
  {
    DoSomething();
  }
  catch(Exception e)
  {
     //do whatever with e
  }
}

public int FooReturnInt()
{
  try
  {
     return IntAfterSomeCalculation();
  }
  catch(Exception e)
  {
    //do exactly whatever with e as foo()
  }
}

Imagine we have a huge class with many public functions like this and we have to apply same try catch in every single function.

Ideally, since the try catch part is identical, and we can pass down Func<> as parameter to a helper function which does something like

public void TryCatch(Func<something> theFunction)
{
  try
  {
    theFunction();
  }
  catch(Exception e)
  {
    //processthe common exception        
  }
}

Then I'd imagine this would tidy up my code alot, the now the problem is how to properly write this function? The return type of this function is depended on the return type of theFunction.

like image 551
Yuan Avatar asked Feb 23 '11 22:02

Yuan


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What do you mean by C?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


2 Answers

If you really think this is needed, you could use:

public T TryCatch<T>(Func<T> theFunction)
{
  try
  {
    return theFunction();
  }
  catch(Exception e)
  {
    // You'll need to either rethrow here, or return default(T) etc
  }
}

However, I would generally advise against it. Are you really sure you need the try/catch blocks in all of these methods? Generally there shouldn't be many try/catch blocks unless the catch just wraps the exception and rethrows... and even that's rarer in C# than it is in Java, for example.

You should usually catch an exception when you can either genuinely handle it gracefully or you need to prevent the process from blowing up just because one request failed (for example). I tend to write very few catch blocks - it's relatively rare that I can really recover from errors :)

This approach is going to lead to a significantly harder time debugging, I suspect. It may still be worth doing, but you should carefully consider the pros and cons first.

like image 171
Jon Skeet Avatar answered Nov 13 '22 09:11

Jon Skeet


There are three reasons to use a catch block:

1 Because there could be an exception.

That is the wrong reason.

2 Because you can somehow handle the Exception.

This is the right reason.

3 Because you want to Add and/or Hide details

This catch & wrap , involves a re-throw and is not really a catch.

The main idea here is that programmers unfamiliar with exception handling tend to use (way) too much catch blocks. You seem to fall in this category.

like image 41
Henk Holterman Avatar answered Nov 13 '22 09:11

Henk Holterman