473,509 Members | 11,437 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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!

Jul 7 '07 #1
4 4133

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

ls****@hotmail.comschreef in bericht
news:%2****************@TK2MSFTNGP02.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!

Jul 7 '07 #2
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.

"Michel Posseth [MCP]" <MS**@posseth.comwrote in message
news:%2****************@TK2MSFTNGP05.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

ls****@hotmail.comschreef in bericht
news:%2****************@TK2MSFTNGP02.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!


Jul 7 '07 #3
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
http://www.coversant.com/blogs/cmullins

"Terry Olsen" <to******@hotmail.comwrote in message
news:u%****************@TK2MSFTNGP04.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("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.

"Michel Posseth [MCP]" <MS**@posseth.comwrote in message
news:%2****************@TK2MSFTNGP05.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

ls****@hotmail.comschreef in bericht
news:%2****************@TK2MSFTNGP02.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!



Jul 7 '07 #4
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

Jul 9 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
15956
by: Jorge Cecílio | last post by:
Hi all! I've a (beginner) problem accessing a method on another class. When I execute a method such as: //response to a swing button public void actionPerformed(ActionEvent event) {
9
1569
by: fabio de francesco | last post by:
Hi, I would like to know why objects that are thrown to be caught are copy-constructed more than once. I have seen this behaviour after running the following simple program that is an...
10
8542
by: Cool Guy | last post by:
Consider: void Start() { if (!TryToDoSomething()) ShowErrorMessage(); }
5
1942
by: Mark Kamoski | last post by:
Hi Everyone-- How can one get the line number of where an error was thrown and/or caught? For example, note the following, for use at any given point in a piece of code: ....to get the...
4
1440
by: chopsnsauce | last post by:
Here is the problem. I'm opening a form that Throw's an error in the Load event and the method thant opens the form has a try..Catch to Catch the error that is thrown in the load event. This...
4
2507
by: Michael Rodriguez | last post by:
I have a data layer in a dll class. If I manually throw an exception in that data layer, the generic Application.OnThreadException in my UI does not catch it nor does it display any message...
1
1191
by: Mark | last post by:
We have an internal web service that intentionally allows raw Exceptions to be thrown to the clients that consume it. This web service is consumed by internal ASP.NET applications. When the...
7
2277
by: Chris | last post by:
Hello all... I have a program with the following structure (all classes mentioned are of my own creation, and none of the classes contain try or catch blocks): - main() consists of a large...
1
2346
by: sudhivns | last post by:
Function F1() throwing an exception of type say int. But this exception is not getting caught. piece of code is: try { .... F1() ..... } catch(int x) {
0
7237
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7137
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7349
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5659
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3219
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3210
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.