Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception propagation in C#

Suppose I have three functions doA(), doB(), and doC() in a C# program where I know that doA() will call doB() which in turn calls doC().

Since doC() has to interact with a database, I know that it could very well generate exceptions that it won't be able to resolve that need to be brought to the user's attention. At the moment, I have the code which might throw the error in a try / catch blow in doC() and then the call to doC() in doB() in another try / catch and similarly the call to doB() in doA() in try / catch block. This allows me to just use throw; to kick the exception up to doA() where something can reasonably be done to display this to the user.

This seems a little like overkill though. I am wondering if since I don't plan on dealing with the exception in doB() or doC() if I can just get rid of the try / catch blocks there.

Assuming there are no finally blocks involved, what is the best practice for dealing with situations like this?

like image 333
chuck taylor Avatar asked Dec 08 '10 19:12

chuck taylor


People also ask

What is exception Propagation?

when an exception happens, Propagation is a process in which the exception is being dropped from to the top to the bottom of the stack. If not caught once, the exception again drops down to the previous method and so on until it gets caught or until it reach the very bottom of the call stack.

What are the exceptions in C?

The C programming language does not support exception handling nor error handling. It is an additional feature offered by C. In spite of the absence of this feature, there are certain ways to implement error handling in C. Generally, in case of an error, most of the functions either return a null value or -1.

What is exception Propagation in C++?

Furthermore, exception handling in C++ propagates the exceptions up the stack; therefore, if there are several functions called, but only one function that needs to reliably deal with errors, the method C++ uses to handle exceptions means that it can easily handle those exceptions without any code in the intermediate ...

How do you propagate checked exceptions?

Exception propagation in Java occurs when an exception thrown from the top of the stack. When it is not caught, the exception drops down the call stack of the preceding method. If it is not caught there, it further drops down to the previous method.


2 Answers

If your catch blocks are just like this:

catch (Exception)
{
    throw;
}

then they are pointless indeed. You're not really handling the exception - don't bother with try/catch at all.

Personally I have very few try/catch blocks in my code - and although there are plenty of implicit try/finally blocks, most are due to using statements.

like image 176
Jon Skeet Avatar answered Oct 06 '22 20:10

Jon Skeet


Yes I would get rid of the try/catch blocks - just let the exception propagate up to the top level and then catch it there. Catching an exception just to rethrow with throw; is simply not useful, although the following variation is actually harmful as it destroys the stack trace information:

catch (Exception exception)
{
    throw exception;
}
like image 33
Mark Byers Avatar answered Oct 06 '22 21:10

Mark Byers