tags:

views:

135

answers:

3

Asserts can't be caught. This is good because some errors I don't want to be wrapped in try/catch, at least not on the development server. But Asserts seem awefully dangerous. If they get onto production, it can hang the ASP.NET server with a msgbox.

//Don't want this on prod even if debug=true is in the web.config
#if DEBUG
    //A future client programmer can wrap this in a try{}catch{}
    if (!EverythingIsOkay)
        throw new InvalidOperationException("Dagnabbit, programming error");

    //This stops the but has less information that an
    // Exception and hangs the server if this accidentally 
    // runs on production
    System.Diagnostics.Debug.Assert(!EverythingIsOkay);
#endif

Is there better way to communicate an violation of a inviolable condition to a developer without risking hanging IIS?

UPDATE: After reading the first replies, I guess the answer hinges on a foolproof way to detect when code is running in a development environment and when it is on a production server, or figuring out how to throw an exception that can't be caught and ignored.

+2  A: 

Don't really see the problem. Do like the first one and throw an exception. Then the server wont hang. Also you should never use debug.Assert in production(as you mentioned).

Throwing an exception will stop the execution aswell and you will be able to catch it. Just make sure that you log the exception and you will be fine with debugging.

Oskar Kjellin
Yes, but if I'm writing code that a future developer (or me) will be calling, I would like to guarantee that the error isn't catch-able. The Debug.Assert(false) is not catchable and cannot be ignored, which is good.
MatthewMartin
The bugs in your code should not be shown to the caller as debug.Assert. It is better to throw an exception or just let it be. You should test your code so well that the debug.Assert is never true and you can take it away.
Oskar Kjellin
A: 

Rather than using the Diagnostics Debug.Assert, could you use a logging tool (i.e. log4net) or a realtime message pump (TCP socket or database) to log the results of a "custom" Assert? Are you wanting to stop executing when the Assert fails?

Dave Swersky
At development time, yes. I guess it comes down to distinguishing between development time and "production" time-- they're both runtime but only in the former would I want to stop. Logs can get ignored, even by well intended developers.
MatthewMartin
+1  A: 

I personally created a class called "Defense" with two methods, "Assert" and "Fail." Assert works similarly to xUnit "assert" in that it takes a boolean condition and a message and throws an exception with the message if the condition is false. Fail throws the exception right away.

It's super-simple and has saved my tuches many, many times. It's ASP.NET friendly and dies hard and fast. If you're concerned about these errors being thrown in the real world you can modify the Assert method so it logs instead using preprocessing directives (#if DEBUG ... #endif), but in my line of work I'd rather see hard errors in the real world than hide them and not know what happened.

roufamatic