C# 安全处理异常时避免第一次机会异常消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/58380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Avoiding first chance exception messages when the exception is safely handled
提问by CVertex
The following bit of code catches the EOS Exception
以下代码捕获 EOS 异常
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
mscorlib.dll 中发生了“System.IO.EndOfStreamException”类型的第一次机会异常
Is there a way to hide these first chance exception messages?
有没有办法隐藏这些第一次机会异常消息?
采纳答案by Brad Wilson
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.
没有什么可担心的。这是正常行为。
回答by DevelopingChris
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.
在不同的范围内添加更多的 try catch 组合,直到您在实际抛出的位置捕获它,但它似乎发生在我们的使用之外,因为流对象不是在使用的范围内创建的。
回答by Alex Duggleby
1) In Visual Studio you can change the settings for the way the Debugger handles (breaks on) exceptions.
1) 在 Visual Studio 中,您可以更改调试器处理(中断)异常的方式的设置。
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.)
转到调试 > 异常。(请注意,根据您的 Visual Studio 环境设置,这可能不在您的菜单中。如果不是,请使用“自定义”菜单将其添加到您的菜单中。)
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.
在“Common Language Runtime Exceptions”这一行中,您可以取消选择throwed(这应该不会再打扰您关于第一次机会的异常),如果愿意,您也可以取消选择User-unhandeled(我不推荐)。
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.
2) 您收到的消息不应出现在控制台中,而应出现在 Visual Studio 的“输出”窗口中。如果是后者,那么我还没有找到删除它的可能性,但是如果您在没有 Visual Studio 的情况下运行应用程序,它就不会出现。
Hope that helps.
希望有帮助。
回答by loudej
Unlike Java, .NET exceptions are fairly expensive in terms of processing power, and handled exceptions should be avoided in the normal and successful execution path.
与 Java 不同,.NET 异常在处理能力方面相当昂贵,在正常且成功的执行路径中应避免处理异常。
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.
您不仅会避免控制台窗口中的混乱,而且您的性能也会得到改善,并且会使像 .NET CLR 异常这样的性能计数器更有意义。
In this example you would use
在本例中,您将使用
while (reader.PeekChar() != -1)
{
bodyByteList.Add(reader.ReadByte());
}
回答by André Chalella
To avoid seeing the messages, right-click on the output window and uncheck "Exception Messages".
为避免看到消息,请右键单击输出窗口并取消选中“异常消息”。
However, seeing them happen might be nice, if you're interested in knowing when exceptions are thrown without setting breakpoints and reconfiguring the debugger.
但是,如果您有兴趣知道何时抛出异常而不设置断点和重新配置调试器,那么看到它们发生可能会很好。
回答by André Chalella
in VB.NET:
在 VB.NET 中:
<DebuggerHidden()> _
Public Function Write(ByVal Text As String) As Boolean
...
回答by AareP
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.
实际上,如果每秒有很多异常,您将通过检查 reader.EndOfStream-value 来获得更好的性能。
回答by Hallgeir Engen
I had this problem and couldn't figure out where the exception was thrown. So my solution was to enable Visual Studio to stop executing on this kind of exception.
我遇到了这个问题,无法弄清楚异常在哪里抛出。所以我的解决方案是让 Visual Studio 停止对这种异常执行。
- Navigate to "Debug/Exceptions"
- Expand the "Common Language Runtime Exceptions" tree.
- Expand the "System" branch.
- Scroll down to where "NullReferenceException" is, and check the "throw" checkbox, and uncheck the "user-handled".
- Debug your project.
- 导航到“调试/异常”
- 展开“公共语言运行时异常”树。
- 展开“系统”分支。
- 向下滚动到“NullReferenceException”所在的位置,选中“throw”复选框,然后取消选中“user-handled”。
- 调试您的项目。
回答by VoteCoffee
If you want more control over these messages, you can add a handler:
如果您想更好地控制这些消息,可以添加一个处理程序:
Friend Sub AddTheHandler()
AddHandler AppDomain.CurrentDomain.FirstChanceException, AddressOf FirstChanceExceptionHandler
End Sub
<Conditional("DEBUG")>
Friend Sub FirstChanceExceptionHandler( source As Object, e As Runtime.ExceptionServices.FirstChanceExceptionEventArgs)
' Process first chance exception
End Sub
This allows you to silence them as mentioned in other comments, but still makes sure you are able to be aware of them. I find it is good to see how many I am really throwing if I log a message and timestamp to a text file.
这使您可以像其他评论中提到的那样使它们静音,但仍然确保您能够意识到它们。我发现如果我将消息和时间戳记录到文本文件中,看看我真正抛出了多少是很好的。