65.9K
CodeProject is changing. Read more.
Home

Show Me Exceptions

Mar 5, 2008

CPOL

1 min read

viewsIcon

35526

downloadIcon

75

How to hide your exceptions in presentations and get them back when developing

The Application

Introduction

Sometimes it’s embarrassing to explain to your boss that these exception messages that pop up with every click on your good looking forms are just “temporary” and for testing purposes. On the other hand, you don't want to make two different copies of the same application, one that is being developed and hence, full of detailed exception messages telling you where and what went wrong, another one which is a clean copy “hiding any unexpected exceptions during runtime” just for meetings and follow up sessions!

If you still don't know what I'm talking about, this may help you get a clearer picture!

Explanation

In this simple application, we're using a simple flag called “ExceptionMode” which we will set to true when we're digging our application alone behind this gray partition.

bool ExceptionMode;

This flag “ExceptionMode” will be set to false or simply ignored during presentations to make sure no embarrassing popups fill the screen during our show.

Properties: Debug>Arguments

Of course, you can also set the parameters from DOS by suffixing your parameter:

C:\>ShowMeExceptions ex

Now if you make an exception in this example, say dividing a number by zero, a nasty message telling you about the sin you have just committed will popup.

The Code

Program.cs

//By Muammar Yacoob
using System;
using System.Collections.Generic;
using System.Windows.Forms; 

internal static class Program
{
    [STAThread]
    private static void Main(string[] args)
    {
        bool exceptionMode = false;
        foreach (string arg in args)
       {
            if (arg.ToLower().Trim() == "ex")
            {
                    exceptionMode = true;
            }
       }
        Application.ThreadException += new EventHandler(Program.HandleException);
        Application.Run(new MainForm());
    }
    private static void HandleException(object sender, ThreadExceptionEventArgs e)
    {
       MessageBox.Show(e.Exception.ToString());
    }
} 

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ShowMeExceptions
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        bool ExceptionMode;
        public Form1(string parameter)
        {
            if (parameter.ToLower() == "ex")
                ExceptionMode = true;

            InitializeComponent();
        }

        private void btn_Devide_Click(object sender, EventArgs e)
        {
            try
            {
                lbl_Result.Text = Convert.ToString(Convert.ToInt32(txt_X.Text) / 
                Convert.ToInt32(txt_Y.Text));
            }
            catch (Exception ex)
            {
                if (ExceptionMode)
                    MessageBox.Show(ex.Message);
            }
        }
    }
} 

Cancelling the Exception

Another way around to do this is to basically cancel the exception from Debug>Exceptions and clear the box under User-unhandled.

Exception list window

Of course, doing so will prevent all the similar exceptions from being caught.

Either ways, what you choose is up to you!

Show Me Exceptions - CodeProject