Click here to Skip to main content
15,884,986 members
Articles / Programming Languages / C#

Show Me Exceptions

Rate me:
Please Sign up or sign in to vote.
2.40/5 (25 votes)
21 Sep 2008CPOL1 min read 34.9K   75   9   9
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.

C#
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

C#
//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

C#
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!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Retired QSoft
Yemen Yemen
Biography?! I'm not dead yet!
www.QSoftOnline.com

Comments and Discussions

 
QuestionHuh? Pin
PIEBALDconsult22-Sep-08 17:55
mvePIEBALDconsult22-Sep-08 17:55 
AnswerMessage Closed Pin
23-Sep-08 1:02
Muammar©23-Sep-08 1:02 
GeneralRe: Huh? Pin
Michal Stehlik23-Sep-08 2:35
Michal Stehlik23-Sep-08 2:35 
QuestionWhy hadle exception on the thread? Pin
Dinesh Mani21-Sep-08 23:53
Dinesh Mani21-Sep-08 23:53 
AnswerMessage Closed Pin
23-Sep-08 1:09
Muammar©23-Sep-08 1:09 
GeneralRe: Why hadle exception on the thread? Pin
Dinesh Mani23-Sep-08 2:09
Dinesh Mani23-Sep-08 2:09 
GeneralTwo things Pin
Ed.Poore5-Mar-08 2:28
Ed.Poore5-Mar-08 2:28 
GeneralFixed! Pin
Muammar©21-Sep-08 23:29
Muammar©21-Sep-08 23:29 
GeneralRe: Two things Pin
Michal Stehlik21-Sep-08 23:49
Michal Stehlik21-Sep-08 23:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.