Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 29-01-2011
Member
 
Join Date: Apr 2008
Posts: 240
Can we have multiple try and Catch block in our program ?

I have been using Microsoft Visual Studio to learn the C# language , today there was my first session of .NET and it was too difficult to understand , and I was being told that I will have to thorough Knowles of C# , I was being told that if I want to a decent developer of .NET then I will have to be proficient in the C# language and today there was a session of error handling , I know the basic part of it and then I came to know about the try and catch block being used to catch the exception being thrown , but for the for time I heard that we can use more than one try an catch block , is it possible , please let me me know about this if it really is I am very eager to know. I will be grateful. I will recommend if a developer or a programmer comment on this .
Reply With Quote
  #2  
Old 29-01-2011
Member
 
Join Date: Apr 2009
Posts: 484
Re: Can we have multiple try and Catch block in our program ?

Is in this case you are on the correct track there are indeed multiple try catch block exist in the code , but if you are not aware , there must be a catch block immediately after a try block if there exist any try block you can have as much as catch block as you can in your clock . Then after that there can be a finally block , these try and catch block work like if and else block , wither of the two execute , but the statement in the finally block the statements are surely executed no matter if any execution takes place in the try and catch block.
Reply With Quote
  #3  
Old 29-01-2011
Member
 
Join Date: May 2009
Posts: 532
Re: Can we have multiple try and Catch block in our program ?

Using a slightly customized edition of Schulz's code:

Code:
try {
try {
A()
} catch (Exception1 obje) {
B()
}
} catch (Exception2  objee){
C()
}

Let us consider  that Exception1 and Exception2 do not  derive inherit from each other.

If A() throws Exception1, B() will run successfully .
If A() throws Exception2, C() will get executed, and B() will not run 
If A() throws Exception3 (which is not caught in this case ), it 'll go up the call
stack.
If A() throws no exception, neither B() nor C()  is going to  run.

If B() throws Exception1, it will will go up the invoke  stack.
If B() throws Exception2, C() will run.

If C() throws some exception, it'll go up the invoke  stack.

The multiple try catch statement can also be displayed in the form of nested try statements , the above code displays the same , if the outer block satisfies then it enters the inner block.
Reply With Quote
  #4  
Old 29-01-2011
Member
 
Join Date: May 2009
Posts: 620
Re: Can we have multiple try and Catch block in our program ?

But the mode it functions, is that an exception thrown in the interior block might be caught by the interior catch, if the type matches. That interior catch can re-throw the exception or not. In any case, the interior block is gone out of scope totally, and control relocates to the external block. If the internal exception is not trapped by the inner block, the inner block's finally still executes but the exception is still going to cut short the external block in that case.
Reply With Quote
  #5  
Old 29-01-2011
Member
 
Join Date: May 2009
Posts: 503
Re: Can we have multiple try and Catch block in our program ?

When an exception is thrown inside a "try" block, the control will be passed to the matching "catch" block. If the category of exception thrown counterparts the category of exception for the "catch", then the catch block will run. Otherwise, the exception will be passed to the "catch" block equivalent to the surrounded "try" block, and handled in a parallel manner. A you said and inquired about , multiple, it is quite possible that the exception being caught are of different nature , so based on their nature they are being caught and it is pretty obvious that the error we encounter in the program are all of different categories , so in order to achieve his we make use of multiple catch block statements . If hope you understood quite clearly and my post was a bit of help for you.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Can we have multiple try and Catch block in our program ?"
Thread Thread Starter Forum Replies Last Post
Block ad in multiple browsers Romany Technology & Internet 4 13-03-2010 10:37 AM
How does C sharp support try-catch-finally block? Trance Maniac Software Development 5 21-01-2010 09:02 AM
Explain Try Catch Block in java cyber-noob Software Development 3 16-11-2009 02:13 PM
Using Try Catch Block In SQL Server 2005 Aaryn Software Development 3 06-06-2009 08:01 PM
Is there any program to block from accessing the Internet? Santanio Technology & Internet 4 23-01-2009 02:36 PM


All times are GMT +5.5. The time now is 12:47 AM.