[Mono-list] How consistent are exceptions?

edward.harvey.mono edward.harvey.mono at clevertrove.com
Wed Feb 13 16:08:13 UTC 2013


I have a dictionary, and I want to read it from one thread, while possibly modifying it from another thread.  In the event when one thread modifies it while another thread is enumerating it, I actually *want* the change to occur, and I want the enumerating thread to be aware a change has occurred.

The best way I know is to let the exception occur, and simply handle the exception.

Problem is, the exception type is InvalidOperationException, which is pretty generic.  So I need to handle InvalidOperationException, and look inside it, to see if "Collection was modified" is the cause of the exception.  Right now, the only way I know to do it is like this:

           catch (InvalidOperationException e)
            {
                if (e.Message == "Collection was modified; enumeration operation may not execute.")

This seems a little kludgy...  First of all, can I be guaranteed the string will remain exactly the same, across platforms, and different versions of .Net and mono?  Second of all, it's a string comparison.  If it works, I don't mind it.  It will happen rarely enough that I'm willing to pay the performance of string comparisons.  The important goal is to accurately and reliably catch the right exception, on different platforms.

Thanks for suggestions...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/mono-list/attachments/20130213/a68f0075/attachment-0001.html>


More information about the Mono-list mailing list