Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
MSIL
Sub HandlerExample()
      Dim intX As Integer
      Dim intY As Integer
      Dim intZ As Integer
      intY = 0
      intX = 5
      ' First Required Error Statement.
      Try
          ' Cause a "Divide by Zero"
          intZ = CType((intX \ intY), Integer)
          ' Catch the error.
      Catch objA As System.DivideByZeroException
          Try
              Throw (New Exception("0 as divisor", objA))
          Catch objB As Exception
              Messagebox.Show(objB.Message)
              Messagebox.Show(objB.InnerException.Message)
              Messagebox.Show(objB.TargetSite.Name)
          End Try
      Catch
          Messagebox.Show("Caught any other errors")
      Finally
          Messagebox.Show(Str(intZ))
      End Try
  End Sub



The question is why we should use Try block inside another Try Block inside another one ...etc ??
I understand the base block but i don't know the others ?
Can you explain to me the the working of sub-Try Block ?
Posted

There is sometimes a need for nested error handling. However, this particular case seems silly. There is no need for a nested Try/Catch in this scenario. You would need a nested Try/Catch if the code in the catch might throw an exception and you have a good reason to catch it.
 
Share this answer
 
Hi,

It is explaining how you can catch different kinds of error with try catch.
In the first try catch, the programmer throw a DivideByZeroException exception. so it is catched on
Catch objA As System.DivideByZeroException.
( otherwise it is catched on
Catch        
 Messagebox.Show("Caught any other errors")
)

now in this catch (DevideByZeroException programmer throw another general exception. so it is catched in
Catch objB As Exception


The Finally block makes sure that the code
Messagebox.Show(Str(intZ)) 
will be executed.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900