How many catch blocks for a class or method?

Posted by Naga116 under C# on 8/4/2011 | Points: 10 | Views : 1933 | Status : [Member] | Replies : 2
How many Catch block can i write for a class or method?

Which block will excute first?

Nagaraju


Responses

Posted by: Hmanjarawala on: 8/4/2011 [Member] Bronze | Points: 25

Up
0
Down

A try block can have multiple catch blocks, each handling a different type of exception. Be careful about the order of laying the exceptions. Using Exception at the top will catch everything that is derived from it, thereby missing the other specfic exception catches. And there can also be only one finally block.

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Naga116, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nithadeepak on: 8/4/2011 [Member] Bronze | Points: 25

Up
0
Down
It is possible to use more than one specific catch clause in the same try-catch statement. In this case, the order of the catch clauses is important because the catch clauses are examined in order. Catch the more specific exceptions before the less specific ones. The compiler produces an error if you order your catch blocks so that a later block can never be reached.(Contents from MSDN)
For more details refer....
http://msdn.microsoft.com/en-us/library/0yd65esw(v=VS.100).aspx

Nitha Deepak

Naga116, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response