Suppose I write some multi-threaded C# code. (I know ... bad idea from the get-go ;) )
I have some code B, which I expect to only get called after code A has completed. I write and review my code carefully to convince myself that this is true, but I can't actually enforce that expectation, I can only check whether it is true.
So I put the checks in, and if the check ever fails, I want to throw and exception so that the developer gets a big shouty log message saying "Nope, ya fucked up; there's still an edge case where the threading doesn't do what you'd convinced yourself it did."
What's the best C# Exception
type to throw here?
My instinct is to go with that old stand-by InvalidOperationException
or possibly just a raw new Exception(message)
. But it would be nice if there were a slightly more specific type I could throw (like throwing an ArgumentException
when that's the issue that's happened)
There are a few Exception
types that auto-complete from Thread
or Sync
but they all look like they're intended for much deeper problems. i.e. there's something wrong with the actual threads. Here the thread are all fine ... it's the developer's threading logic that's in error.
Are there any sensible pre-existing Exception
classes to use here?