throw/handle exceptions while using backgroundworker

C

chris

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

Jay B. Harlow [MVP - Outlook]

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


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

chris

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

Jay B. Harlow [MVP - Outlook]

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


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

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