Results 1 to 5 of 5

Thread: Can we have multiple try and Catch block in our program ?

  1. #1
    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 .

  2. #2
    Join Date
    Apr 2009
    Posts
    488

    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.

  3. #3
    Join Date
    May 2009
    Posts
    539

    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.

  4. #4
    Join Date
    May 2009
    Posts
    637

    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.

  5. #5
    Join Date
    May 2009
    Posts
    511

    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.

Similar Threads

  1. Block ad in multiple browsers
    By Romany in forum Technology & Internet
    Replies: 4
    Last Post: 13-03-2010, 10:37 AM
  2. How does C sharp support try-catch-finally block?
    By Trance Maniac in forum Software Development
    Replies: 5
    Last Post: 21-01-2010, 09:02 AM
  3. Explain Try Catch Block in java
    By cyber-noob in forum Software Development
    Replies: 3
    Last Post: 16-11-2009, 02:13 PM
  4. Using Try Catch Block In SQL Server 2005
    By Aaryn in forum Software Development
    Replies: 3
    Last Post: 06-06-2009, 08:01 PM
  5. Is there any program to block from accessing the Internet?
    By Santanio in forum Technology & Internet
    Replies: 4
    Last Post: 23-01-2009, 02:36 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,711,046.58347 seconds with 17 queries