Throw

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pbala
    New Member
    • Dec 2008
    • 37

    Throw

    What is the Usage of Throw in a Catch File????
  • nukefusion
    Recognized Expert New Member
    • Mar 2008
    • 221

    #2
    Do you mean code like the following?

    Code:
    Try
        ' Do stuff here
    Catch DivByZero As System.DivideByZeroException
        ' Catch divide by zero exceptions
    Catch OtherException As Exception
        Throw OtherException
    End Try
    It's called rethrowing the exception and you might do that if you decide that the currenty try - catch block doesn't know how to deal with that particular exception

    In this case rethrowing the exception causes it to be passed up to the next try - catch block in the chain. So, say the above code was a function you had written called DoStuff() and you've wrapped the call to DoStuff() in another try - catch block, the rethrown exception would get passed back to that block.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Re-throwing an exception isn't the only use for a Throw.

      If you are developing a control and you need to inform the calling code that something has gone wrong you could throw an exception.

      For instance, if your Person object requires a name before doing something with it, you could throw an exception if the calling code attempts to use the Person object before setting a name. In this case, designing a custom exception is a good idea to indicate that it is a problem originating from the Person object.

      Check out this article for more information on designing a custom exception.

      Comment

      • sashi
        Recognized Expert Top Contributor
        • Jun 2006
        • 1749

        #4
        Hi there,

        Error handling!!

        Comment

        Working...