QUIZ: What's Wrong With This Code?

This is a simplified version of a sneaky bug I ran into today (I’m fine thank you, but the bug is dead). The only prize I can offer is a GMail account if you want one.

Imagine that the method HandleRedirect actually does something interesting and if all the conditions pass, the user is redirected to special.aspx. This is the source code for an HttpHandler implemented as a .ashx file.

<%@ WebHandler Language="C#" Class="MyHandler" %>
using System;
using System.Web;
 
public class MyHandler : IHttpHandler
{
    /// <summary>
    /// Processs an incoming request.
    /// </summary>
    public void ProcessRequest(HttpContext ctx)
    {
        try
        {
            HandleRedirect(ctx);
        }
        catch(Exception)
        {
            ctx.Response.Redirect("/default.aspx");
        }
    }
 
    void HandleRedirect(HttpContext ctx)
    {
        ctx.Response.Redirect("/special.aspx");
    }
 
    public bool IsReusable
    {
        get { return true; }
    }
}
Technorati Tags: ,

What others have said

Requesting Gravatar... Daniel Turini Nov 17, 2004 6:47 PM
# RE: QUIZ: What's Wrong With This Code?
My guesses:
1. The Class="SwitchHandler" is wrong, it should be MyHandler.
2. Never, never, catch (Exception) and don't log it. Actually, avoid catch (Exception), unless your only purpose it to log it.
Requesting Gravatar... Brad Wilson Nov 17, 2004 8:33 PM
# re: QUIZ: What's Wrong With This Code?
Yep, I've run into this, too.

A hint: the way he's calling Response.Redirect, it will result in a call to Response.End.

By the way, there's a related oddity in HTTP handler factories. Ever run across that one?
Requesting Gravatar... Haacked Nov 18, 2004 12:46 AM
# re: QUIZ: What's Wrong With This Code?
Daniel: 1. See what happens when you try to take a real-world problem and convert it into a sample. ;) That was a typo and not the intended "problem". But good catch. I fixed that. 2. Ok, that's a guideline that I violate in order to make this sample simple. However, there's a bigger problem with this code.

Brad: Good hint!
Requesting Gravatar... BigJimSlade Nov 18, 2004 1:25 AM
# re: QUIZ: What's Wrong With This Code?
Response.Redirect will alsway fire thread aborted exception i beleive there is an method overload that modifies this behavoir
Requesting Gravatar... Haacked Nov 18, 2004 1:54 AM
# re: QUIZ: What's Wrong With This Code?
That's it!
Requesting Gravatar... you've been HAACKED Nov 18, 2004 2:28 AM
# Quiz Answer: Watch out for the Eeeevil Thread.Abort.
Requesting Gravatar... you've been HAACKED Apr 09, 2007 4:27 PM
# T-SQL Quiz - Dealing With Trends
T-SQL Quiz - Dealing With Trends
Requesting Gravatar... Community Blogs Apr 09, 2007 5:02 PM
# T-SQL Quiz - Dealing With Trends
I’m not one to post a lot of quizzes on my blog. Let’s face it, while we may create altruistic reasons

What do you have to say?

(will show your gravatar)
Please add 5 and 5 and type the answer here: