Throw
In an exception handling we use try and catch. In some occasion in addition to try and catch we use throw as well. Could you tell me please when throw is necessary?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
VulpesRank #55 · 29.2K reputationPosted Jul 31, 2013, 3:12 AM EDT
Check this link for more detail:
http://msdn.microsoft.com/en-us/library/1ah5wsex(v=vs.71).aspx
VulpesRank #55 · 29.2K reputationPosted Jul 31, 2013, 4:30 AM EDT
Notice that by allowing the try/catch in the Main() method to handle the exception, you don't need to repeat the code in the catch block of the Divide() method:
The output is:
Posted Jul 31, 2013, 4:46 AM EDT
VulpesRank #55 · 29.2K reputationPosted Jul 31, 2013, 5:22 AM EDT
However, in general, the try/catch which finally handles a re-thrown exception just needs to be in a method higher up the call stack. As the Main() method is the first method called it's always at the top of the call stack.
Where you have nested try/catch statements in the same method and an exception is rethrown from an 'inner' catch, it can also be caught in an 'outer' try/catch without the need to look for a suitable try/catch higher up the call stack.
Posted Jul 31, 2013, 9:19 AM EDT