确定哪个代码行引发了异常
在 dotNet 中,一行抛出异常并被捕获,我如何找出哪个文件中的哪一行抛出了异常? 看起来比较简单,但我无法弄清楚......
In dotNet a line throws an exception and is caught, how can I figure out which line in which file threw the exception? Seems relatively straightforward, but I can't figure it out...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
仅当有可用的调试符号时才可以执行此操作。
如果你想在 VS 中调试这种情况,你最好只在位于
Exceptions
对话框中选中Common Language Runtime Exceptions
的Thrown
复选框。调试
菜单。 一旦抛出异常,调试器就会中断,即使它位于try
块中。You can only do it if you have debug symbols available.
If you want to debug such a situation in VS, you'd better just check
Thrown
checkbox forCommon Language Runtime Exceptions
inExceptions
dialog located inDebug
menu. The debugger will break as soon as the exception is thrown, even if it's in atry
block.就我个人而言,我只是记录异常的 ToString() 返回值。 包括整个堆栈跟踪。 这是一行代码......非常简单。
Personally, I just log the exception's ToString() return value. The whole stack trace is included. It's one line of code ... dead simple.
您可以使用 StackFrame 类:
You could use the StackFrame Class:
嗯,在 .NET 中,有所谓的 FirstChanceException。 这些本质上是在处理异常之前抛出的。 有两种方式来看待您在此提出的问题。 一是从调试角度。 如果进行调试,您只需将调试器设置为从“调试/异常”窗口捕获抛出的异常。 这在交互式环境中更容易。 如果您需要在非交互式上下文中记录此信息,那么我会执行类似于 CMS 正在讨论的操作...
这里唯一的区别是我们获取整个堆栈跟踪,然后转到第一帧,其中是最初抛出异常的地方。
Well, in .NET you have whats called a FirstChanceException. These essentially are thrown before an exception is handled. There are two ways of looking at the issue that you're presenting here. One is from a debugging angle. If debugging you can just set your debugger to catch thrown exceptions from the Debug/Exceptions window. This is easier in an interactive context. IF you need to record this information from within a non-interactive context then I would do something similar to what CMS is talking about...
The only difference here is that we get the entire Stack Trace, then go to the first frame, which is where the exception was originally thrown.