views:

4628

answers:

8

The following bit of code catches the EOS Exception

using (var reader = new BinaryReader(httpRequestBodyStream)) {

    try {
        while (true) {
            bodyByteList.Add(reader.ReadByte());
        }
    } catch (EndOfStreamException) { }
}

So why do I still receive first-chance exceptions in my console?

A first chance exception of type 'System.IO.EndOfStreamException' occurred in mscorlib.dll

Is there a way to hide these first chance exception messages?

A: 

I think the stream is throwing this exception, so your try is scoped to narrow to catch it.

Add a few more try catch combos around the different scopes until you catch it where its actually being thrown, but it appears to be happening either at our outside of your using, since the stream object is not created in the using's scope.

DevelopingChris
+15  A: 

The point of "first-chance" exceptions is that you're seeing them pre-handler so that you can stop on them during debugging at the point of throwing. A "second-chance" exception is one that has no appropriate handler. Sometimes you want to catch "first-chance" exceptions because it's important to see what's happening when it's being thrown, even if someone is catching it.

There's nothing to be concerned with. This is normal behavior.

Brad Wilson
Indeed, it is nothing to be concerned with, but they do clutter up the debug output log :(
Dimitri C.
+6  A: 

1) In Visual Studio you can change the settings for the way the Debugger handles (breaks on) exceptions.

Go to Debug > Exceptions. (Note this may not be in your menu depending on your Visual Studio Environment setting. If not just add it to your menu using the Customize menu.)

There you are presented with a dialog of exceptions and when to break on them.

In the line "Common Language Runtime Exceptions" you can deselect thrown (which should then stop bothering you about first-chance exceptions) and you can also deselect User-unhandeled (which I would not recommend) if want to.

2) The message you are getting should not be in the console, but should be appearing in the 'Output' window of Visual Studio. If the latter is the case, then I have not found a possibility to remove that, but it doesn't appear if you run the app without Visual Studio.

Hope that helps.

AlexDuggleby
+3  A: 

Unlike Java, Dotnet exceptions are fairly expensive in terms of processing power and handled exceptions should be avoided in the normal and successful execution path. Not only will you avoid clutter in the console window but your performance will improve and it will make performance counters like .NET CLR Exceptions more meaningful.

In this example you would use

while (reader.PeekChar() != -1)
{
    bodyByteList.Add(reader.ReadByte());
}
loudej
or you could get all the bytes in one shot with ReadBytes and also make use of buffering. but I guess that wasn't the question.
Craig Tyler
Doesn't answer the question
André Neves
Sure it does. "Is there a way to hide these first chance exception messages?" - the first chance exceptions would not appear with this loop. :)
loudej
+13  A: 

To avoid seeing the messages, right-click on the output window and uncheck "Show exceptions" (the name may not be exactly that, I don't have VS here right now).

However, seeing them happen might be nice, if you're interested in knowing when exceptions are thrown without setting breakpoints and reconfiguring the debugger.

André Neves
+1 for being the only one to answer the asked question
Peter Wone
The exact name is "Exception Messages". Thanks for answering the second half of the question.
StriplingWarrior
A: 

ChanChan ever heard of the stacktrace in a exception? :P that will be atleast 100% faster to use if you want to know where its thrown but i dont think thats the problem here....

A: 

in VB.Net...

<DebuggerHidden()> _
Public Function Write(ByVal Text As String) As Boolean
   ...
Hal
This has nothing to do with the question.
John Saunders
Actually, using [DebuggerNonUserCode] *does* hide "first chance exception" messages. I wouldn't be surprised DebuggerHidden also does this.
Ruben
I'm not sure if these are considered exceptions, but [DebuggerNonUserCode] won't hide any of the "Managed Debugging Assistants". For example, I get BindingFailures when I use XmlSerializer, and I've yet to find a way to hide it, other than unchecking BindingFailure when Thrown from the Exceptions dialog.
Anthony Brien
A: 

Actually if are having many exceptions per second, you would achieve must better performance by checking reader.EndOfStream-value.. Printing out those exception messages is unbelievably slow, and hiding them in visual studio won't speed up anything.

AareP