3

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?

Community
  • 1
  • 1
MrWuf
  • 1,478
  • 1
  • 14
  • 30

2 Answers2

2

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

Micah Armantrout
  • 6,781
  • 4
  • 40
  • 66
2

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

leppie
  • 115,091
  • 17
  • 196
  • 297