Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
we use throw ex & only throw & say rethrowing an exception...then why not is there a keyword of rethrow in c#...?
what is the exactly theory behind of rethrowing?


one level & higher level mean as e pass exception message to for example data layer to business layer or like this ....??/
Posted
Updated 25-Apr-11 0:19am
v2

If your question is why would people catch an exception and re-throw it, then one very common situation where this is done is for logging. The exception is caught, a log entry added, and then rethrown. The calling code may suppress the exception but the log will still have the error referenced.

Another situation is to selectively throw under certain circumstances, and in others to silently swallow it or throw a wrapped exception.

There are other possible uses too but these two should make it clear enough for you I hope.
 
Share this answer
 
The exception re-throw (just "throw" without argument) of very useful, but it is semantic-free. Nothing about business layer and other absurdity. Is it continues propagation of exception up the stack.

Basically, you need to catch all the exception only on top of each thread, to prevent termination of the application; in all other cases you need to re-throw, just to prevent blocking of exception from propagation. There is a limited number of cases when propagation is blocked (exception is "finally" processed), usually as a work-around of some "bad" third-party code which you cannot improve.

Usually, you only need to re-throw when you add some processing for a specific case but it does not fix the problem completely, so you do only the processing your code is competent about at this point and propagate processing up the stack to leave it to a more general handler. If you don't do this partial processing, you should not catch it. Very typically, you need to catch one type of exception and throw another one.

Usual mistake is not wrong handling an exception but catching it without the purpose. Very typical completely wrong style is catching all exception in some function. Having some code in a separate function is a very bad reason for catching. A rule is: if you don't know what to do with a concrete type of exception, don't catch it. But you always need to catch only on top of the stack of each thread. Catching exception in UI thread is separate, see below.

For further detail, please see my instruction on using exceptions:
How do i make a loop that will stop when a scrollbar reaches the bottom[^],
When i run an application an exception is caught how to handle this?[^].

—SA
 
Share this answer
 
Comments
Monjurul Habib 25-Apr-11 18:27pm    
seems helpful, my5
Sergey Alexandrovich Kryukov 25-Apr-11 18:40pm    
Thank you, Monjurul. It should be.
--SA
I don't quite understand your question. To rethrow an exception that is caught in a try/catch block do something like the following:

try
{
    throw new Exception();
}
catch(Exception ex)
{
    throw ex;
}


There is no need for a rethrow keyword, just throw the exception again. I have used this when producing websites as frequently when an exception is thrown you want to handle it where it occurred then pass it on up the chain so it can be displayed to the user.
 
Share this answer
 
Comments
Sandeep Mewara 25-Apr-11 7:29am    
To keep track of what OP said to you:
you said, then pass it on up the chain so it can be displayed to the user
that is why we use the word rethrowing?



Your asnwer to OP:
This should be a comment not a solution and no you don't need rethrow. Rethorw would be the exact same as throw! Passing it on is merely catching it then throwing it again which is exactly the same as what you would have to do with a rethrow keyword. Your concept is one and the same as the throw keyword.
Ed Nutting 25-Apr-11 7:31am    
Thanks :)
Sergey Alexandrovich Kryukov 25-Apr-11 10:37am    
This code is strictly equivalent to:
throw new Exception();
(!)
Also, you don't need "throw ex", just "throw" without arguments.
Re-throwing is used for adding local processing of a exception with further propagation, even more typically, for processing more narrow class of exception (NOT System.Exception).
This answer makes no sense and misleads.
--SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900