【问题标题】:Visual studio source of message in output window输出窗口中的 Visual Studio 消息源
【发布时间】:2019-02-01 18:52:41
【问题描述】:

假设我的输出窗口中有一条消息(显示 Debug 的输出)

抛出异常:mscorlib.dll 中的“System.InvalidOperationException”

我的应用程序不会抛出异常,只是显示该消息并继续。当我调用导入的 DLL 中的方法时发生异常,该 DLL 是 C++ 模块(我的应用程序是 C#)。尽管出现此消息,但该方法似乎仍能正常运行。

我的猜测是该模块正在处理异常,然后显示该消息,但我想确定是这种情况,并且与我如何导入它或编组数据(或那个自定义编组器)无关)。

(我的代码:

[DllImport("theExternalModule.dll", EntryPoint = "ReadData", SetLastError = true)]
[return: MarshalAs(UnmanagedType.U4)]
private static extern UInt32 ReadData([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(JaggedArrayMarshaler))] Int16[][] data,
                                      [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(JaggedArrayMarshaler))] Int16[][] dataOrig,
                                      [MarshalAs(UnmanagedType.U2)] Int16 buffsize,
                                      ref int smpNum);

resultCode = ReadData(_buffer, _origBuffer, bufferSize, ref sampleNumber);

(当我在调试器中单步执行此行时,会显示消息)

我的问题是,有没有办法让输出窗口告诉我是什么模块或方法导致显示该消息?

【问题讨论】:

  • 请查看主菜单调试/异常。您可以定义 VS 调试器将停止(中断)的异常。这可用于仅在引发异常时停止调试,并且您可以检查调用堆栈以获取所需的信息。 (可能是菜单项的名称略有不同。-我是从我的德语 VS 翻译过来的,即不知道它们在英文本地化 VS 中是如何称呼的。)
  • @colmde, 异常设置能否帮助您捕获身边的异常?

标签: c# c++ visual-studio debugging visual-studio-2017


【解决方案1】:

此问题通常在创建枚举器后更改集合时发生。您最初的问题没有足够的代码来断言,但您可以阅读有关此问题的信息 herehere

这是一个会引发异常的示例(取自其中一篇文章):

List<Book> books = new List<Book>();
books.Add(new Book("The Stand", "Stephen King"));
books.Add(new Book("Moby Dick", "Herman Melville"));
books.Add(new Book("Fahrenheit 451", "Ray Bradbury"));
books.Add(new Book("A Game of Thrones", "George R.R. Martin"));
books.Add(new Book("The Name of the Wind", "Patrick Rothfuss"));

Book newBook = new Book("Robinson Crusoe", "Daniel Defoe");
foreach (var book in books)
{
  if (!books.Contains(newBook))
  {
    books.Add(newBook);  // Throws a InvalidOperationException in mscorlib.dll
  }
}

您可能还希望查看the InvalidOperation class on MSDN 以了解更多原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-06
    • 2013-01-21
    • 1970-01-01
    • 2010-10-08
    相关资源
    最近更新 更多