473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

throw/handle exceptions while using backgroundworker

hello, I can't seem to make this work:
VS2005
I have a simple program that uses a backgroundworker control to execute a
long process (webservice call)

if that webservice call fails, i want to raise an exception and display a
messagebox.

however no matter where i put the Try block to trap the error, it does not
work.

the only example i could find on it is here:
http://msdn2.microsoft.com/en-us/library/4852et58.aspx

in which case they do this:

Private Sub backgroundWorker1_RunWorkerCompleted( _
ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) _
Handles backgroundWorker1.RunWorkerCompleted

' First, handle the case where an exception was thrown.
If Not (e.Error Is Nothing) Then
MessageBox.Show(e.Error.Message)
ElseIf e.Cancelled Then

however, i can't figure out how to set the e.Error in the
RunWorkerCompletedEventArgs.

that same example from the msdn site has the following lines of code in the
procedure that's being called by the backgroundworker:

' The parameter n must be >= 0 and <= 91.
' Fib(n), with n > 91, overflows a long.
If n < 0 OrElse n > 91 Then
Throw New ArgumentException( _
"value must be >= 0 and <= 91", "n")
End If

but this example does not work. the actual numericUpDown control in the
form is set to not allow any values outside those parameters, so the
exception condition ( If n < 0 OrElse n > 91 ) is never true. if you
actually passs something outside of those values to the procedure, you get
the following error when the "Throw New ArgumentException..." line is
executed:

ArgumentException was unhandled by user code.

this is the same problem that i have.

to repeat this, copy the example code from
http://msdn2.microsoft.com/en-us/library/4852et58.aspx

and change the Maximum property on the numericUpDown1 control to something
greater than 91.

run the program, enter the value 92 in the numericUpDown1 control, and click
Start.

the Throw New ArgumentException line generates an error.

does anybody know how to get the e.Error in the RunWorkerCompletedEventArgs
to work?

thanks,
chris.


Jan 25 '06 #1
3 12090
Chris,
| does anybody know how to get the e.Error in the
RunWorkerCompletedEventArgs
| to work?
Simply throwing (or allowing) an exception to be thrown in the DoWork event
will cause that event to appear in the RunWorkerCompletedEventArgs.Error
property.

Private Sub backgroundWorker1_DoWork(ByVal sender As Object, ByVal e As
DoWorkEventArgs) Handles BackgroundWorker1.DoWork

Throw New ArgumentException("Silly me, something must be wrong")

End Sub

NOTE: VS does not see the error as handled, so it will display its dialog
box first, if you simply click run, the code in RunWorkerCompleted will then
display the above message.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"chris" <.@.> wrote in message news:ERzBf.324637$2k.115362@pd7tw1no...
| hello, I can't seem to make this work:
| VS2005
| I have a simple program that uses a backgroundworker control to execute a
| long process (webservice call)
|
| if that webservice call fails, i want to raise an exception and display a
| messagebox.
|
| however no matter where i put the Try block to trap the error, it does not
| work.
|
| the only example i could find on it is here:
| http://msdn2.microsoft.com/en-us/library/4852et58.aspx
|
| in which case they do this:
|
| Private Sub backgroundWorker1_RunWorkerCompleted( _
| ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) _
| Handles backgroundWorker1.RunWorkerCompleted
|
| ' First, handle the case where an exception was thrown.
| If Not (e.Error Is Nothing) Then
| MessageBox.Show(e.Error.Message)
| ElseIf e.Cancelled Then
|
| however, i can't figure out how to set the e.Error in the
| RunWorkerCompletedEventArgs.
|
| that same example from the msdn site has the following lines of code in
the
| procedure that's being called by the backgroundworker:
|
| ' The parameter n must be >= 0 and <= 91.
| ' Fib(n), with n > 91, overflows a long.
| If n < 0 OrElse n > 91 Then
| Throw New ArgumentException( _
| "value must be >= 0 and <= 91", "n")
| End If
|
| but this example does not work. the actual numericUpDown control in the
| form is set to not allow any values outside those parameters, so the
| exception condition ( If n < 0 OrElse n > 91 ) is never true. if you
| actually passs something outside of those values to the procedure, you get
| the following error when the "Throw New ArgumentException..." line is
| executed:
|
| ArgumentException was unhandled by user code.
|
| this is the same problem that i have.
|
| to repeat this, copy the example code from
| http://msdn2.microsoft.com/en-us/library/4852et58.aspx
|
| and change the Maximum property on the numericUpDown1 control to something
| greater than 91.
|
| run the program, enter the value 92 in the numericUpDown1 control, and
click
| Start.
|
| the Throw New ArgumentException line generates an error.
|
| does anybody know how to get the e.Error in the
RunWorkerCompletedEventArgs
| to work?
|
| thanks,
| chris.
|
|
|
|
Jan 25 '06 #2
You're right!

in the compiled application it works fine. only in vs does the error
appear, and if you click run, it continues and works properly.

(too bad this wasn't more obvious to me ;)

thanks, Jay.

"chris" <.@.> wrote in message news:ERzBf.324637$2k.115362@pd7tw1no...
hello, I can't seem to make this work:
VS2005
I have a simple program that uses a backgroundworker control to execute a
long process (webservice call)

if that webservice call fails, i want to raise an exception and display a
messagebox.

however no matter where i put the Try block to trap the error, it does not
work.

the only example i could find on it is here:
http://msdn2.microsoft.com/en-us/library/4852et58.aspx

in which case they do this:

Private Sub backgroundWorker1_RunWorkerCompleted( _
ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) _
Handles backgroundWorker1.RunWorkerCompleted

' First, handle the case where an exception was thrown.
If Not (e.Error Is Nothing) Then
MessageBox.Show(e.Error.Message)
ElseIf e.Cancelled Then

however, i can't figure out how to set the e.Error in the
RunWorkerCompletedEventArgs.

that same example from the msdn site has the following lines of code in
the procedure that's being called by the backgroundworker:

' The parameter n must be >= 0 and <= 91.
' Fib(n), with n > 91, overflows a long.
If n < 0 OrElse n > 91 Then
Throw New ArgumentException( _
"value must be >= 0 and <= 91", "n")
End If

but this example does not work. the actual numericUpDown control in the
form is set to not allow any values outside those parameters, so the
exception condition ( If n < 0 OrElse n > 91 ) is never true. if you
actually passs something outside of those values to the procedure, you get
the following error when the "Throw New ArgumentException..." line is
executed:

ArgumentException was unhandled by user code.

this is the same problem that i have.

to repeat this, copy the example code from
http://msdn2.microsoft.com/en-us/library/4852et58.aspx

and change the Maximum property on the numericUpDown1 control to something
greater than 91.

run the program, enter the value 92 in the numericUpDown1 control, and
click Start.

the Throw New ArgumentException line generates an error.

does anybody know how to get the e.Error in the
RunWorkerCompletedEventArgs to work?

thanks,
chris.

Jan 25 '06 #3
Chris,
The error appears in VS, as the default under "Debug - Exceptions" is to
break when an exception is user-unhandled for Common Language Runtime
Exceptions. If you uncheck "User-unhandled in "Debug - Exceptions" then it
should give you the same results in VS.

VS doesn't always consider exception handlers built-in to the Framework. The
DoWork has a exception handler around it, so it can notify your main thread.
VS doesn't realize this, so it thinks the exception is unhandled. Hence the
dialog box...

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"chris" <.@.> wrote in message news:jQOBf.219701$tl.217237@pd7tw3no...
| You're right!
|
| in the compiled application it works fine. only in vs does the error
| appear, and if you click run, it continues and works properly.
|
| (too bad this wasn't more obvious to me ;)
|
| thanks, Jay.
|
| "chris" <.@.> wrote in message news:ERzBf.324637$2k.115362@pd7tw1no...
| > hello, I can't seem to make this work:
| > VS2005
| > I have a simple program that uses a backgroundworker control to execute
a
| > long process (webservice call)
| >
| > if that webservice call fails, i want to raise an exception and display
a
| > messagebox.
| >
| > however no matter where i put the Try block to trap the error, it does
not
| > work.
| >
| > the only example i could find on it is here:
| > http://msdn2.microsoft.com/en-us/library/4852et58.aspx
| >
| > in which case they do this:
| >
| > Private Sub backgroundWorker1_RunWorkerCompleted( _
| > ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) _
| > Handles backgroundWorker1.RunWorkerCompleted
| >
| > ' First, handle the case where an exception was thrown.
| > If Not (e.Error Is Nothing) Then
| > MessageBox.Show(e.Error.Message)
| > ElseIf e.Cancelled Then
| >
| > however, i can't figure out how to set the e.Error in the
| > RunWorkerCompletedEventArgs.
| >
| > that same example from the msdn site has the following lines of code in
| > the procedure that's being called by the backgroundworker:
| >
| > ' The parameter n must be >= 0 and <= 91.
| > ' Fib(n), with n > 91, overflows a long.
| > If n < 0 OrElse n > 91 Then
| > Throw New ArgumentException( _
| > "value must be >= 0 and <= 91", "n")
| > End If
| >
| > but this example does not work. the actual numericUpDown control in the
| > form is set to not allow any values outside those parameters, so the
| > exception condition ( If n < 0 OrElse n > 91 ) is never true. if you
| > actually passs something outside of those values to the procedure, you
get
| > the following error when the "Throw New ArgumentException..." line is
| > executed:
| >
| > ArgumentException was unhandled by user code.
| >
| > this is the same problem that i have.
| >
| > to repeat this, copy the example code from
| > http://msdn2.microsoft.com/en-us/library/4852et58.aspx
| >
| > and change the Maximum property on the numericUpDown1 control to
something
| > greater than 91.
| >
| > run the program, enter the value 92 in the numericUpDown1 control, and
| > click Start.
| >
| > the Throw New ArgumentException line generates an error.
| >
| > does anybody know how to get the e.Error in the
| > RunWorkerCompletedEventArgs to work?
| >
| > thanks,
| > chris.
| >
| >
| >
| >
|
|
Jan 25 '06 #4

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

Similar topics

5
by: | last post by:
Unfortunately I've used exception handling as a debugging tool. Now I want to be smarter about handling errors. Today in the global.asx in the Application_OnError event, I inserted code to email...
3
by: Tim Anderson | last post by:
I've been experimenting with the BackgroundWorker class. As a test, I've built an application that searches for files matching a RegEx and populates a listbox with the results. My approach is to...
4
by: Boni | last post by:
Dear all, is it possible to handle 2 exception tipes in one block? I.e Try if XXX then
10
by: tcomer | last post by:
Hello! I've run into a problem and I can't seem to understand "why" the problem occurs. In my program: using System; using System.IO; using System.Xml; namespace MyNamespace { class MainApp
5
by: Michael M. | last post by:
I have the following code (listed at bottom of post) that pings a small range of IP address to see which ones are alive. To speed things up a little I am trying to use more than one thread,...
1
by: beginner | last post by:
Hi All, I am wondering if there is any way to handle exceptions inside list comprehension. For example, I want to skip the point if f(x) raises an exception. How can I do that without...
0
by: iowna uass | last post by:
Hi, I'm converting a dll from VB6 to VS2008 and I have a question on when exception handling should be used. The dll is a custom module that integrates security into our corporate applications...
6
by: Author | last post by:
I am writing a small library in C#, which will be compiled to dll and shared by multiple applications. This is sorta new to me. I am wondering how professionals handle exceptions in a dll...
0
by: =?Utf-8?B?RGFya3Jpc2Vy?= | last post by:
I have managed code in C# calling unmanaged code from a DLL written in C++. Every time an Exception is thrown by unmanaged code, this exception is handled by C# directly. How to force to handle...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.