Thrown Exception not being Caught

T

Terry Olsen

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!
 
M

Michel Posseth [MCP]

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/micro...eptions+posseth&rnum=4&hl=nl#f60428fc2290c143


Michel




"Terry Olsen" <to

(e-mail address removed)> schreef in bericht
 
T

Terry Olsen

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("MODE READER")
GetNntpResponse()
If rslt <> 201 And rslt <> 200 Then
ShutDown()
Throw New Exception(_NNTPServer & ": " & 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.message)
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.
 
C

Chris Mullins [MVP]

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.
 
C

Chris Dunaway

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 MyCustomExceptionWithAdditionalInformation(Ex)
End Try

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top