Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Re-throwing Exceptions

Tags:

c#

.net

exception

When throwing exceptions between multiple methods, should all methods re-throw the exception? for example

Method1()
{
   Method2();
}

Method2()
{
   try
   {
      // Do something
   }
   catch
   {
      throw;
   }
}

try
{
   Method1();
}
catch
{
   // Do something about exception that was thrown from Method2()
}

Notice how in Method1(), I didn't need to wrap Method2() in a try block, should I be?

like image 518
Scott Avatar asked Jul 09 '11 09:07

Scott


People also ask

What C is 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 ...

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.

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.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

You don't need to wrap everything in try blocks.

You should only try when you want to catch something, and you should only catch something in the following cases:

  • You're ready to handle the exception (do whatever needs to be done and don't let it propagate up the stack),
  • You want to do something with the exception (e.g. log it) before rethrowing it (by using the parameterless form of throw),
  • You want to add details to the exception by wrapping it in another exception of your own (see Allon Guralnek's excellent comment below).
like image 60
Frédéric Hamidi Avatar answered Sep 23 '22 22:09

Frédéric Hamidi


You do not need to try, catch, and rethrow exceptions unless you have some particular reason for catching them in the first place. Otherwise, they'll automatically get bubbled up from the lower level functions that throw them to the highest level function in your code. Essentially, you can think of them as getting "rethrown" all the way up, even though this isn't technically what is happening.

In fact, most of the time that you see a try/catch block written, it's incorrect. You should not catch exceptions unless you can actually handle them. It's utterly pointless (and in fact considered to be bad practice) to catch exceptions just to rethrow them. Do not wrap all of your code within try blocks.

Note that by "handle them", I mean that your code in the catch block will take some specific action based on the particular exception that was thrown that attempts to correct the exceptional condition.
For example, for a FileNotFoundException, you might inform the user that the file could not be found and ask them to choose another one.

See my answer here for more detail and a thorough discussion of "exception handling best practices".

like image 35
Cody Gray Avatar answered Sep 22 '22 22:09

Cody Gray