How To Disable “A first chance exception of type…” Messages in VS 2005
Well, all those
A first chance exception of type 'System.ArgumentException' occurred in MyAssembly.dll
messages in my Immediate window during the debugging annoyed me for a long time. Today, I said: enough! There must be a way how to tell VS not to show them.
We know that first chance exceptions don’t necessarily need to be dangerous. See What is a First Chance Exception? and First and second chance exception handling. I’m using exceptions very often and I handle all of them very carefully. There never appears second chance exception in my code. I’m getting exceptions when trying to add duplicate item to hashtable or trying to get the item with the key which doesn’t exist in custom collection. Doing it in countless loops, I’m getting hundreds of warnings at debug time. Some may say that this is poor design. Yes, I could use millions of If ... Then checks to avoid exceptions but I think that avoiding this is one of the exceptions purposes.
So how do we suppress the messages? You most likely see these messages in Immediate Window because you have set Redirect all Output Window text to the Immediate Window in Tools - Options… - Debugging - General. That’s why when you right-click in Immediate Window, you cannot see any option to disable messages. It’s Output Window text and thus you need to set it in the Output Window. So open Output Window, right-click and uncheck Exception Messages from context menu. Simple but it took me one hour to find it.




January 7th, 2007 at 2:04 am
Thank you thank you thank you! That’s been driving me nuts for absolutely ages. Amazing - such a simple solution to ease my headaches. You are a certified genius
March 15th, 2007 at 9:57 pm
Big fanx! I have hated those “first chance exception” for ages, but haven’t thought of the possibility to turn them off until now.
April 11th, 2007 at 12:16 am
Thanks for this useful bit of information. I have a question regarding exceptions in C#. I’ve only recently come over to .NET from many years of Java programming. Exceptions in Java are slightly different, if you try and call code that will throw an exception you MUST handle it (in one of two ways). This does not seem the case in C#. Is there an way to discover if a method throws and exception? In Java the method declaration included the exception type(s) thrown.
Cheers
Q
April 11th, 2007 at 9:06 am
You are right. AFAIK, there is no way to determine whether the method throws an exception in .NET. Unlike Java, the .NET compiler doesn’t force you to catch exception or explicitly declare it in the method signature. It would be great if it worked Java way because our VSdocman could automatically generate <exception> tags for the method comments. Moreover, I’m not aware of any add-in that can find method exceptions. Maybe I should play with it
May 4th, 2007 at 11:58 am
Actually you can just add some XML document above your methods… this will give others some indication of what your code throws:
///
/// This method does something
///
/// If this happens this exception is thrown
Gives you some information when you watch the method info trough the object browser and stuff… offcourse, this is just extra information you don’t HAVE to give.
May 4th, 2007 at 1:13 pm
To Aaron Lenoir:
> /// If this happens this exception is thrown
Yes, that’s what <exception> tag in XML comment is for. But the point was, how do you know, which <exception> tags you should add to your method? What if your method calls 20 other methods, where each of them throws say 3 exceptions? If you don’t catch those exceptions, they will bubble to your method’s caller. So you need to find MSDN documentation for each method you are using to see which exceptions they throw. Then you need to add all those exceptions in your XML comment. This is not very convenient.
As mentioned, Java compiler doesn’t allow this. It will force you to either catch every possible exception or explicitly define which exceptions your method will bubble.
May 26th, 2007 at 4:50 pm
If you only knew how often I googled this to find the answer. I dupplicated your answer with a variation on your title on my blog. Maybe other people find it as well.
Thanks!
July 17th, 2007 at 10:57 am
****************** Excellent! great Job Master!!!!!!!!!!!!!!!!!! my 3 cheers
August 9th, 2007 at 9:32 pm
This is a good thought, however that also eliminates ALL other exceptions that really do matter. To many people hided errors by putting a generic catch that does nothing. Also, this would hide useful exceptions like trying to update a control while in a thread; an all too common problem in managed code. Atleast VS2005 warns you with the exception, but not if you prevent exceptions from being displayed.
February 5th, 2008 at 7:17 pm
Thank you for this tip! I’ve had this “A first chance exception of type ‘System.Net.Sockets.SocketException’ occurred in System.dll” trash obscuring my output window for far too long (making it almost impossible to find the output I really care about).
Too bad VS doesn’t provide a more useful filter. As mkmcgregor points out you have to do an ALL or NOTHING filter on exceptions in the output window, but you can still set the Debug:Exceptions to break your code in the debugger for the ones you care about, and ignroe the rest.
Thanks again!
June 22nd, 2008 at 11:07 pm
Holy Hannah! This is a great tip! I only wish I wasn’t so lazy for so long to look for a solution that you had posted for so long! Thank you so much!
July 7th, 2008 at 11:39 am
Hello…Great Job Man……. : )
July 7th, 2008 at 11:48 am
Hi Friends, Am developing a driver and what i need is to share a block of memory like 16MB between my kernel driver and User applications…..
My SDMA will write into that memory block directly and my applications will just read that data and diagnose it.
Am using AllocPhysMem() API in my kernel driver but at the time while am running my application which calls my driver, an exception like “First-chance exception K.COREDLL.DLL….0xC0000005 ” is appearing.
I dont know what to do…I even googled for many hours but unable to figure it out….
Please, please, anybody please help me…..
“The exeption is appearing exactly at point when AllocPhysMem API is called”
Any reply is appreciated
July 7th, 2008 at 11:52 am
No way boy, i think nobody here can able to answer your question…..
Try any another forum….
September 8th, 2008 at 11:47 pm
Thanks for the tip.
One other thing you may want to do to actually find the problem of a first time exception by going to Debug -> Exceptions… and check the Thrown checkboxes. Then debug your code and find where the exception occurs.
September 15th, 2008 at 11:30 am
Dis does not, however, improve performance. It will only hide the messages.
November 6th, 2008 at 12:58 am
It is my understanding that exceptions are computationally expensive to create since the stack has to be walked. While your technique will hide the messages you are, no doubt, paying a significant performance penalty for using exceptions to control the flow of your code. Exceptions are really supposed to be for “exceptional” circumstances.
I realize that, in your case, the number of loop iterations that cause an exception might make up a very small part of the over all iteration count so maybe you wouldn’t see a difference in performance. I just want to make sure people realize that depending on exceptions to control program flow is highly discouraged.
January 27th, 2009 at 9:28 pm
Thank you so much! You have saved my life. Come to Waterloo and I will buy you doughnuts.
June 3rd, 2009 at 3:23 pm
I don’t agree with turning off first chance exceptions. Sure many times you know what they are and can be over looked but you should investigate when this happens. If not for breaking, but even for efficiency.
An example would be inserting the same object into a list. It will throw a first chance and handle it but you might have a bug/inefficiency that is requesting the same object to be inserted twice, or maybe you really need it to take in two copies of the object.
Turning these off will make you totally unaware of a potential conflict, and it’s only in the debugger so what do you care if they pop up.
August 13th, 2009 at 6:29 am
Was driving me crazy trying to Debug.Writeline some info in a catch statement and could not see my stuff for all the “A first chance exception” messages. Thanks for the article
October 26th, 2009 at 11:09 am
Thanks
January 30th, 2010 at 7:09 pm
Many thanks! It was so annoying…
March 15th, 2010 at 11:14 pm
Thanks for yet another poor soul who was suffering from a flood of these worthless messages. You rock!
July 23rd, 2010 at 4:28 pm
Hello and thank you for the valuable information. The “first-chance” exception dialog might become very annoying too (it pops up very often sometimes). Do you know hot to turn it out in VS2005?