Recently I was working on an application that uses utilities provided by another DLL. The function I was using creates a custom connection manager based on the destination database.
It seemed every time the connection manager was invoked, it would generate this error.
A first chance exception of type 'System.InvalidOperationException' occurred in System.dll
A 'first chance exception' message notifies you there was an exception in one of your intervening layers. The exception was handled in the layer; therefore it was handled at the first chance.
If you debugger gets a second chance exception, your application will typically crash.
http://support.microsoft.com/default.aspx?scid=kb;en-us;105675
My application was running as expected, but generated the above message in my immediate window; obviously annoying.
I stepped through the code and didn't notice any anomalies, at first I thought this was generated erroneously.
My application is a procedural audit utility to verify data at different times in a manufacturing process, therefore I use this function 40+ times per application execution; it would generate this error on every execution.
As a result of my testing, I thought about disabling the output to the immediate window since the exception appeared to be handled inside the function.
http://www.helixoft.com/blog/archives/24
On second thought, I wanted to know exactly what was generating this message and see if this could be avoided; who knows what pain this exception may cause in the future.
I had access to the source code, so I changed my DLL reference to a project reference so I could walk through the code.
As it turned out, the function was doing a call looking for a AppKey in my App.config.
If the key didn't exist in the App.config, ithe code threw an exception, then the exception would assign a default value.
<exception code>
try
Dim boolRunMe As Boolean
'below causes a NullReferenceException when calling .ToString on an empty Configuration attribute
boolRunMe = CType(ConfigurationManager.AppSettings("RunMe").ToString, Boolean)
catch ex as Exception
boolRunMe = False
end Try
</exception code>
For some people setting this value in a exception may be a suitable solution; but what if something else threw the exception? In this case, the least of my worries would be to assign a default value. In this case, you may want to catch specific exceptions.
For me, I want to avoid an exception at this point since this function is called many times; throwing exceptions at this frequency is less efficient than other methods (we have run internal tests on multiple exception throwing).
The code I implemented to verify the key rather than forcing an exception to handle this scenario
<AppSetting key test code>
Dim boolRunMe As Boolean
'Avoid an exception by check for the existence of the attribute
If String.IsNullOrEmpty(ConfigurationManager.AppSettings("RunMe")) Then
boolRunMe = False
Else
boolRunMe = CType(ConfigurationManager.AppSettings("RunMe").ToString, Boolean)
End If
</AppSetting key test code>
Lesson learnt:
I have seen my share of Visual Studio bugs so I was quick to write this off as 'by design'.
If you have access to any suspect code, do yourself and your team a favor; walk through the code to review if this odd behavior is by 'your design'.
Cheers
Gary Pronych