How do I safely Debug.Assert in ASP.NET?
- by MatthewMartin
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.