MrWuf
MrWuf

Reputation: 1498

First-chance SocketException getting thrown but my catch isn't working

I have a WCF application that sometimes performs long running queries. I have created code on the client side that understands when a Timeout on the socket has occurred and will take steps to repoll the server later to grab the answer. The system throws a number of first-chance exceptions just before the TimeoutException occurs including SocketException. In the past, first-chance exceptions have slowed my apps considerably until I caught and handled them.

I want to get rid of these first-chance exceptions so I created a try-catch block to handle the SocketException and turned on the exceptions in VS2010 and reran the code. When I did, I saw the debugger show me that the SocketException had occurred, but my catch block was never entered. (see below)

enter image description here

I found a number of similar posts including C# SocketException doesn't get caught but none of the answers I found address how to catch the Exception, or whether I should even worry about it.

So my questions are:

1) Should I be worrying about these first chance exceptions?

2) If so, how do I get the system to catch them?

Upvotes: 3

Views: 1002

Answers (2)

leppie
leppie

Reputation: 117290

First chance exceptions are thrown exceptions that are already caught.

In other words, it is impossible to catch a first chance exception.

You also dont have to worry about them (generally).

Upvotes: 2

Micah Armantrout
Micah Armantrout

Reputation: 6981

There is an event you can attach to called AppDomain.FirstChanceException Event

this is how you would connect to it:

  AppDomain.CurrentDomain.FirstChanceException += FirstChanceHandler;

it is on the app domain the application is running on here is the documentation on it. http://msdn.microsoft.com/en-us/library/system.appdomain.firstchanceexception.aspx

Upvotes: 2

Related Questions