Exception has been thrown by the target of an invocation

FuZion

Well-known member
Joined
Jan 28, 2007
Messages
47
Programming Experience
Beginner
I have a server/client application, and I keep receiving this strange error on the server side.

VB.NET:
Exception has been thrown by the target of an invocation.

Any idea what this could mean? It doesn't give me any info as to where the error is originating from... Although it has started happening ever since I've added a System.Net.WebClient to the program. Would it help if I posted code for this?

Thanks a lot!

FuZion
 
Does it still happen when you remove the WebClient call?
Is it happening when multithreading?
Have you tried enabling Debug > Exceptions, break on Thrown for all CLR exceptions, if so did this bring you closer to where the code breaks?
 
VB.NET:
Exception has been thrown by the target of an invocation.

What it means is: a method was run using invocation (a way of running a method without necessarily knowing its name. Event handlers are an example of this - they dont have to be a fixed name, but its possible to write a method that is called when a button is clicked) and that method experienced an error

Because invocations dont necessarily take place on the same thread, the computer cant always be sure what happened.. it just knows that an error occurred.

Follow john's advice and enable your app to break on all exceptions that are thrown, not jsut unhandled ones.

As soon as the method being invoked, experiences the error, the code should halt..
 
Ok I just did that and I'm going to run it right now and sweet what happens. I'm pretty sure it does not happen when I remove the webclient. Also, the webclient is in the workCompleted(something like that) event of a background orker. The background worker is called through a timer.

I'll let post when the error is thrown, it usually takes a few minutes.
...
Ok the exception thrown was The browser has timed out.

So what can I do to fix this?
 
try/catch it

I se now why the "Exception by target of invoke" is thrown... The background worker does things on a different thread, so when one of those things throws an exception, if it is unchecked, it bubbles up into the main thread (the one that started the background worker) but as it crosses the boundary, it merely becomes an invoke-target-exception (the background worker's method is the invokation target)

trap it close to where it occurrs...

i.e. if you say:

WebCLient.DownloadFile(url)

and it eventually times out.. then wrap it:

VB.NET:
try
  WebCLient.DownloadFile(url)
catch whateverexception
  do something here, maybe backgroundworker.ReportProgress(0, "failed")
end try
 
Back
Top