By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,588 Members | 1,207 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,588 IT Pros & Developers. It's quick & easy.

throw/handle exceptions while using backgroundworker

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.