When writing code, I'll quite often temporarily inject random exceptions to ensure error flow is as expected.
eg
public void SomeFunc()
{
Console.WriteLine("Some code");
throw new Exception("BOOOOM!"); //added temporarily
Console.WriteLine("Some more code");
}
The problem is I have warnings set as errors, so compilation will fail with a CS0162 Unreachable code detected, since "Some more code" will never run.
So just add a condition you say:
public void SomeFunc()
{
Console.WriteLine("Some code");
if (true)
throw new Exception("BOOOOM!"); //added temporarily
Console.WriteLine("Some more code");
}
but no, this compiler is clever enough to note that the condition will always be true, and flags a CS0162 again.
I generally end up with the following:
public void SomeFunc()
{
Console.WriteLine("Some code");
var debug = true;
if (debug)
throw new Exception("BOOOOM!"); //added temporarily
Console.WriteLine("Some more code");
}
So my idle question, since I'm lazy, is whether there's a simpler way to fool the compiler? A one liner would be perfect.
(And yes, I do write unit tests eventually ;)