Thrown Exception not being Caught

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Terry Olsen

    #1

    Thrown Exception not being Caught

    I have a class where I throw a new exception from a method

    My calling code (in a different class) is wrapped in a Try/Catch block, but
    it's not catching. Instead the IDE is highlighting the ThrowNew Exception
    line in the class and saying it was unhandled.

    Looking for some help.

    Thanks!



  • Michel Posseth  [MCP]

    #2
    Re: Thrown Exception not being Caught


    Terry
    My calling code (in a different class) is wrapped in a Try/Catch block,
    but it's not catching. Instead the IDE is highlighting the ThrowNew
    Exception line in the class and saying it was unhandled.
    maybe you are handling specific errors and the one you raise is not one of
    them ?


    IMHO it is foolish to throw a new exception this way

    try
    catch ex as exception
    throw new exception("bla" )
    end try

    i do not see the point of catching the error at all in the dll if you
    want it to bubbel up to the caller, you could just let the chaining
    mechanism handle this , errors are always chained
    just as in VB6,
    so if you write a handler in a top level of the chain the error will be
    catched there with much less overhead and more information about the origin


    see my posting and the responses here




    Michel




    "Terry Olsen" <to

    lsen64@hotmail. comschreef in bericht
    news:%23oZr7XMw HHA.3364@TK2MSF TNGP02.phx.gbl. ..
    >I have a class where I throw a new exception from a method
    >
    My calling code (in a different class) is wrapped in a Try/Catch block,
    but it's not catching. Instead the IDE is highlighting the ThrowNew
    Exception line in the class and saying it was unhandled.
    >
    Looking for some help.
    >
    Thanks!
    >
    >
    >

    Comment

    • Terry Olsen

      #3
      Re: Thrown Exception not being Caught

      I'm not throwing an exception in an exception. Here's a snippet...

      Public Class NNTP
      Public Sub Stuff
      ..code removed for brevity
      'Tell server that we're a reader
      sw.WriteLine("M ODE READER")
      GetNntpResponse ()
      If rslt <201 And rslt <200 Then
      ShutDown()
      Throw New Exception(_NNTP Server & ": " & rsltStr)
      End If
      End Sub
      End Class

      From my main window, i'm calling it like this:

      Dim nntp As New NNTP(ServerName )
      Try
      nntp.Stuff
      Catch ex as Exception
      msgbox(ex.messa ge)
      Exit Sub
      End Try

      However, when Sub Stuff throws the exception, the calling code isn't
      catching it. Instead, it says that the thrown exception was unhandled.

      If you know how to properly do this, i'd appeciate some pointers.

      "Michel Posseth [MCP]" <MSDN@posseth.c omwrote in message
      news:%23M4NvGNw HHA.1208@TK2MSF TNGP05.phx.gbl. ..
      >
      Terry
      >
      >My calling code (in a different class) is wrapped in a Try/Catch block,
      >but it's not catching. Instead the IDE is highlighting the ThrowNew
      >Exception line in the class and saying it was unhandled.
      >
      maybe you are handling specific errors and the one you raise is not one of
      them ?
      >
      >
      IMHO it is foolish to throw a new exception this way
      >
      try
      catch ex as exception
      throw new exception("bla" )
      end try
      >
      i do not see the point of catching the error at all in the dll if you
      want it to bubbel up to the caller, you could just let the chaining
      mechanism handle this , errors are always chained
      just as in VB6,
      so if you write a handler in a top level of the chain the error will be
      catched there with much less overhead and more information about the
      origin
      >
      >
      see my posting and the responses here
      >

      >
      >
      Michel
      >
      >
      >
      >
      "Terry Olsen" <to
      >
      lsen64@hotmail. comschreef in bericht
      news:%23oZr7XMw HHA.3364@TK2MSF TNGP02.phx.gbl. ..
      >>I have a class where I throw a new exception from a method
      >>
      >My calling code (in a different class) is wrapped in a Try/Catch block,
      >but it's not catching. Instead the IDE is highlighting the ThrowNew
      >Exception line in the class and saying it was unhandled.
      >>
      >Looking for some help.
      >>
      >Thanks!
      >>
      >>
      >>
      >
      >

      Comment

      • Chris Mullins [MVP]

        #4
        Re: Thrown Exception not being Caught

        I would guess one of two things are happening:
        1 - You're calling "Stuff" from inside the constructure or your NNTP class.
        With you constructing your NNTP class outside the try/catch, the exception
        is being propigated.

        2 - You have "Break on all Exceptions" turned on. This means each time an
        exception is thrown, you break into the debugger. Look at this via
        "Ctrl-Alt-E" at design time, and make sure the settings are all default.

        There area also a few exception types that you really can't catch - for
        example if you run out of memory, or run out of stack space, it's not always
        possible to catch these. I really doubt you're running into that here
        though.

        --
        Chris Mullins, MCSD.NET, MCPD:Enterprise , Microsoft C# MVP
        Find your domain name at HugeDomains. Start using this domain right away.


        "Terry Olsen" <tolsen64@hotma il.comwrote in message
        news:u%2338QpNw HHA.1184@TK2MSF TNGP04.phx.gbl. ..
        I'm not throwing an exception in an exception. Here's a snippet...
        >
        Public Class NNTP
        Public Sub Stuff
        ..code removed for brevity
        'Tell server that we're a reader
        sw.WriteLine("M ODE READER")
        GetNntpResponse ()
        If rslt <201 And rslt <200 Then
        ShutDown()
        Throw New Exception(_NNTP Server & ": " & rsltStr)
        End If
        End Sub
        End Class
        >
        From my main window, i'm calling it like this:
        >
        Dim nntp As New NNTP(ServerName )
        Try
        nntp.Stuff
        Catch ex as Exception
        msgbox(ex.messa ge)
        Exit Sub
        End Try
        >
        However, when Sub Stuff throws the exception, the calling code isn't
        catching it. Instead, it says that the thrown exception was unhandled.
        >
        If you know how to properly do this, i'd appeciate some pointers.
        >
        "Michel Posseth [MCP]" <MSDN@posseth.c omwrote in message
        news:%23M4NvGNw HHA.1208@TK2MSF TNGP05.phx.gbl. ..
        >>
        >Terry
        >>
        >>My calling code (in a different class) is wrapped in a Try/Catch block,
        >>but it's not catching. Instead the IDE is highlighting the ThrowNew
        >>Exception line in the class and saying it was unhandled.
        >>
        >maybe you are handling specific errors and the one you raise is not one
        >of them ?
        >>
        >>
        >IMHO it is foolish to throw a new exception this way
        >>
        >try
        >catch ex as exception
        >throw new exception("bla" )
        >end try
        >>
        >i do not see the point of catching the error at all in the dll if you
        >want it to bubbel up to the caller, you could just let the chaining
        >mechanism handle this , errors are always chained
        >just as in VB6,
        >so if you write a handler in a top level of the chain the error will be
        >catched there with much less overhead and more information about the
        >origin
        >>
        >>
        >see my posting and the responses here
        >>
        >http://groups.google.nl/group/micros...0428fc2290c143
        >>
        >>
        >Michel
        >>
        >>
        >>
        >>
        >"Terry Olsen" <to
        >>
        >lsen64@hotmail. comschreef in bericht
        >news:%23oZr7XM wHHA.3364@TK2MS FTNGP02.phx.gbl ...
        >>>I have a class where I throw a new exception from a method
        >>>
        >>My calling code (in a different class) is wrapped in a Try/Catch block,
        >>but it's not catching. Instead the IDE is highlighting the ThrowNew
        >>Exception line in the class and saying it was unhandled.
        >>>
        >>Looking for some help.
        >>>
        >>Thanks!
        >>>
        >>>
        >>>
        >>
        >>
        >
        >

        Comment

        • Chris Dunaway

          #5
          Re: Thrown Exception not being Caught

          IMHO it is foolish to throw a new exception this way
          >
          try
          catch ex as exception
          throw new exception("bla" )
          end try
          >
          This example would not be a good example, but sometimes you want to
          catch an exception and then throw a different exception, perhaps a
          custom exception with additional information in it. In which case,
          you would wrap the original exception in your new one:

          Try
          'Something that might throw and exception
          Catch Ex As Exception
          Throw New MyCustomExcepti onWithAdditiona lInformation(Ex )
          End Try

          Chris

          Comment

          Working...