Click here to Skip to main content
16,004,192 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I found this question and also answer but didn't understand that.
Quote:
Yes.
The exceptions which cant be handled in the defined catch block are thrown to its caller.

C#
catch

{

    throw;

    // to throw specific exception

    // throw new ArgumentException("this Error occured");

}


Can anyone please help me to understand this.

Thanks in advance.
Posted
Comments
bbirajdar 10-Jul-12 14:19pm    
Nothing difficult in this.. Try creating a program yourself in Visual Studio and you will understand it better than somebody explaining it to you...
Sergey Alexandrovich Kryukov 10-Jul-12 14:21pm    
Understand what? You either use "throw" to re-throw exactly what was caught or "throw new..." to throw a different exception.
--SA

1 solution

Please see my comment to the question.

You are probably confused by your quote, which is purely idiotic. There is not such thing as "throw to the caller", because exceptions do no have caller. To understand how they work, you should understand how a call stack works. You have one call stack in each thread; and this is the only mechanism of calling methods and passing parameters. When you enter a try block, the point of the stack is remembered for the current thread. If, you enter another try block insider, another try point is remembered, with all needed execution context. All those try points are collected in a separate stack "try stack", the collection of try points. The position of those points is purely a run-time entity. When an exception is thrown, the systems find the closest try point up the stack and restores the call stack to this point.

This is like a time machine. The exception mechanism jumps execution back in time to the last remembered point as the exception was never thrown. Then you can handle it and re-throw. If you re-throw, the execution jumps back the the previous try point, up to the very top of the call stack. That's why it is important to catch all exceptions on top of the stack of each thread.

This is not so easy to understand in depth for a person who is used to regular call/return mechanism. Exception, as well as threading, is the mechanism working "over" the call stack; it breaks the stack rules in a safe way, jumping only back in "time" and restoring complete execution context as it was left at the past moment of time, when execution enters "try". Failure to understand this mechanism cause developers to over-process exception. Exception mechanism was designed to create a "throw-and-forget" system, very good for isolation of "regular" behavior from the "exceptional" one. Most of the development activity should be made in assumption that exceptional situations never happen, because they can be handled separately, in isolation from the "regular" behavior.

—SA
 
Share this answer
 

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