none
Visual basic "TRY/CATCH" in "TRY/CATCH" (Multiple instances) RRS feed

  • Question

  • Hey guys.

    I hope you all know what try does, and what I am trying to do is merge another "try" in 1 of the try built already. Okay, let me explain it.

    This is what my normal code looks like:

    Try
        While something = True
    
    'MY CODE
    
        End While
    
    End try
    
    Catch ex As Exception
    
    'MY CODE
    
    end try


    This is what I am trying to do

    Try
        While something = True
    
    'MY CODE
    
        End While
    
    End try
    
    Catch ex As Exception
    
    'ANOTHER TRY
    
    Try
    while somethingelse = true
    
    'MY CODE
    
    END while
    
    end try
    
    Catch ex As Exception
    
    End try

    so basically I am trying to insert "TRY" followed with "Catch ex As Exception" in 1 that is built already.

    Obviously this wouldn't work out because it would keep on going for ever. 

    ** Basically, I got this idea from if statement. So lets say it was IF statement. I am trying to insert something like elseif so I could put multiple things in, but I cannot find one for try statement.

    I hope you guys understand

    any help?

    Wednesday, February 29, 2012 8:17 PM

Answers

  • It's difficult to understand what you are trying to do, but it is permissible to nest try catch blocks.  Your syntax was just a little off:

            Try
                'your code here
            Catch ex As Exception
                Try
                    'your other code here
                Catch ex As Exception
     
                End Try
            End Try
    

    • Marked as answer by Y0MANNN Wednesday, February 29, 2012 10:07 PM
    Wednesday, February 29, 2012 9:12 PM

All replies

  • That doesn't make sense to me.  Your existing structure cannot be what you have described because the required format for Try/Catch is 
      Try
      Catch
      End Try

    What you have described for your present structure is invalid.  If you use the correct structure then you can string together as many sequential Try/Catch as you want.

    Wednesday, February 29, 2012 9:07 PM
  • It's difficult to understand what you are trying to do, but it is permissible to nest try catch blocks.  Your syntax was just a little off:

            Try
                'your code here
            Catch ex As Exception
                Try
                    'your other code here
                Catch ex As Exception
     
                End Try
            End Try
    

    • Marked as answer by Y0MANNN Wednesday, February 29, 2012 10:07 PM
    Wednesday, February 29, 2012 9:12 PM
  • It's difficult to understand what you are trying to do, but it is permissible to nest try catch blocks.  Your syntax was just a little off:

            Try
                'your code here
            Catch ex As Exception
                Try
                    'your other code here
                Catch ex As Exception
     
                End Try
            End Try
    

    This was exactly what I wanted. Thanks so much, worked fine. 
    • Marked as answer by Y0MANNN Wednesday, February 29, 2012 10:07 PM
    • Unmarked as answer by Y0MANNN Wednesday, February 29, 2012 10:07 PM
    Wednesday, February 29, 2012 10:07 PM