6

Let's say I have a message in my output window (Showing output from Debug)

Exception thrown: 'System.InvalidOperationException' in mscorlib.dll

My application doesn't throw an exception, just displays that message and carries on. The exception occurs when I make a call to a method in an imported DLL which is a C++ module (my application is C#). The method still appears to function correctly in spite of this message appearing.

My guess is that that module is handling the exception and then displaying that message, but I want to be sure that this is the case and it's nothing to do how I've imported it or marshalled the data (or that custom marshaller).

(My code:

[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);

and

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

(when I step through this line in the debugger, the message is displayed)

My question is, is there a way of getting the output window to tell me what module or method caused that message to be displayed?

2
  • 2
    Please, have a look at main menu Debug/Exceptions. You can define the exceptions on which the VS debugger will stop (break). This can be used to stop debugging just when an exception is thrown, and you can inspect the callstack to get the required info. (May be, the menu items are named slightly differently. - I translated them from my German VS, i.e. don't know how they are called in the English localized VS.) Commented Aug 27, 2018 at 11:28
  • @colmde, Can the Exception setting help you capture the exception in your side? Commented Sep 1, 2018 at 5:39

1 Answer 1

0

This problem usually happens when you change a collection after an enumerator has been created. Your original question don't have enough code to assert that, but you can read about this problem here and here.

Here's an example (taken from one of the article) that would throw that exception :

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
  }
}

You may also wish to take a look at the InvalidOperation class on MSDN for further causes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.