What’s Wrong With This Code: Exceptions

This is my first post on a series of posts that are going to be devoted to seeing if you can spot the problem with this code. All code is REAL code in production or close to going live. Here is the first one:

Not properly handling Exceptions is .NET is very important part of coding your application. Not doing so can bring your app to a crashing halt! So lets see if you can spot the problem with the code below:

        try
        {
            ctrl = (Control)Parent.FindControl("SomeControl");
            if (ctrl != null)
            {
                PrepareControlForExport(ctrl);
                ExportControl();
            }
        }
        catch (Exception ex)
        {
        }

Send in your response by using comments. Will post answer later.

3 thoughts on “What’s Wrong With This Code: Exceptions

  1. Besides the fact that you have semicolons in your VB… you might as well drop the nullability check because it’s going to blow up before that and get swallowed by the exception block. 😉

    1. I would say that you don’t handle the exception correctly. No code is asociated with the catch. You should at least display an error message in log to inform the user of the error that occured.
      And btw, this is not VB, this is C# 🙂

  2. One thing I see is that like Jim Wooley said, the exception is thrown out. It’s ‘caught’, but never actually handled. Some might also say that this code does not catch /known/ exception types, but rather just catches anything that might go (critically) wrong with .Net. To some ways of thinking, a critical system exception should not be caught in code, but rather allowed to terminate execution or bubble up to a higher level exception handler. I can see someone saying that the Catch() block should catch specific exception types. Personally, I see this as debatable.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.