Click here to Skip to main content
Licence CPOL
First Posted 4 Mar 2008
Views 18,545
Downloads 23
Bookmarked 9 times

Show Me Exceptions

By | 21 Sep 2008 | Article
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!

License

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

About the Author

Muammar©

Retired
QSoft
Yemen Yemen

Member

Biography?! I'm not dead yet!
www.QSoftOnline.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHuh? PinmemberPIEBALDconsult17:55 22 Sep '08  
AnswerRe: Huh? PinmemberMuammar©1:02 23 Sep '08  
Confused | :confused: Confused | :confused: I really dont understand why some people just dont get the purpose of this article!
 
I know it's just stupid to ignore run-time exceptions but this's not what the article is about, I did say it's for testing without embarrassment when your boss suddenly walks in and tell you "Please show me what you've been doing so far". So instead of telling 'em, oh, this error message is because the app is not ready yet but it wont be there once I'm done with it!! Take for example, an array out of bounds exception that happens when populating the last item in a combobox, no body will realize if you just hide it and deal with it later! but having this big ding sound with an ugly error message might bring some doubts to your boss that things are not going fine.
 

PIEBALDconsult wrote:
Or at least consider that management might tell you to put it into production immediately... imagine the hole you'll be in then!

 
Laugh | :laugh: Laugh | :laugh: .. Don't worry, I'll tell them it's not ready yet and what I've been showing you is the beta version that cant be put to practice yet!
 
Last word, I'm truly sorry if you dont like the article and I actually thought of deleting it, but when I first published it, I thought it would bring much relief for those who needed such a trick "like me!" I actually use this trick and I'm comfortable!
 
Anyways, thanks for your valuable comment mateSmile | :)
 

All generalizations are wrong, including this one!
(\ /)
(O.o)
(><)

GeneralRe: Huh? PinmemberMichal Stehlik2:35 23 Sep '08  
QuestionWhy hadle exception on the thread? PinmemberDinesh Mani23:53 21 Sep '08  
AnswerRe: Why hadle exception on the thread? PinmemberMuammar©1:09 23 Sep '08  
GeneralRe: Why hadle exception on the thread? PinmemberDinesh Mani2:09 23 Sep '08  
GeneralTwo things PinmemberEd.Poore2:28 5 Mar '08  
GeneralFixed! PinmemberMuammar©23:29 21 Sep '08  
GeneralRe: Two things PinmemberMichal Stehlik23:49 21 Sep '08  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120604.1 | Last Updated 22 Sep 2008
Article Copyright 2008 by Muammar©
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid