Click here to Skip to main content
13,249,443 members (32,709 online)
Click here to Skip to main content
Add your own
alternative version


57 bookmarked
Posted 18 May 2007

Exception Message Box Using C#

, 18 May 2007
Rate this:
Please Sign up or sign in to vote.
Allows applications to display detailed and formatted error messages
Screenshot - DetailErrorMessage.jpg


Exceptions are unavoidable factors in development. Here I explain how to display the exception occurred in a detailed and in the best formatted way.

This displays a message box that can be customized with text, buttons, and symbols to improve the customer experience with a Microsoft Windows .NET Framework.

ApplicationExceptionMessagebox helps any developer to display an error message in the best formatted way. The user is given a choice to add up user defined messages as shown in DetailErrorMessage.jpg. ExceptionMessageBox gives the user an option whether to display the same message again and again in the application. Like the usual MessageBox in C#, we have an option to display a button as per the need. In the above example, I have used only Yes and No buttons.


The user is requested to add a .NET reference named ExceptionMessageBox and a namespace using Microsoft.SqlServer.MessageBox;

Using the Code

I have created a solution named ExceptionMessageBoxSample. First I added a Windows Form named Form1.cs to my solution. On the form load, I am implicitly throwing an exception which will be caught by my catch block.

STEP 1: Add the .NET Reference ExceptionMessagebox and add the namespace in the code:

//using Microsoft.SqlServer.MessageBox;

STEP 2: The try-block contains the guarded code block that may cause the exception. The block is executed until an exception is thrown or it is completed successfully. Here I am implicitly throwing an exception.

// Do something that you don't expect to generate an exception.
throw new ApplicationException
    ("An unexpected error occurred. Please call Helpdesk."); 

STEP 3: The catch clause can be used without arguments, in which case it catches any type of exception, and is referred to as the general catch clause. It can also take an object argument derived from System.Exception, in which case it handles a specific exception.

catch (ApplicationException ex)
                // Define a new top-level error message.
                string str = "Write the reason why the action failed.";

                // Add the new top-level message to the handled exception.
                ApplicationException exTop = new ApplicationException(str, ex);
                exTop.Source = this.Text;
                // Information in the Data property of an exception that has a name
                // beginning with "HelpLink.Advanced" is shown when the user
                // clicks the Advanced Information button of the exception message
                // box dialog box.
                exTop.Data.Add("AdvancedInformation.FilePosition", "line 24");
                    "a detail message can be given");

                // Show the exception message box with additional information that
                // is helpful when a user calls technical support.
                ExceptionMessageBox box = new ExceptionMessageBox(exTop);
                box.Buttons = ExceptionMessageBoxButtons.YesNo;
                box.Caption = "Caption";                
                box.ShowCheckBox = true;
                box.ShowToolBar = true;
                box.Symbol = ExceptionMessageBoxSymbol.Stop;

Add the new top-level message to the handled exception ApplicationException. The string variable str is used to display the ApplicationException source.

Now we are all done. Now you can execute the program and see the result.

Points of Interest

ExceptionMessage box does have pre-defined properties. Some of them are:

  • Beep: Specifies whether to play an audible sound when the message is displayed
  • Data: Gets the IDictionary interface that stores help link and advanced information associated with the top-level message
  • Option: Gets or sets miscellaneous display options for the message box
  • ShowCheckBox: Specifies whether to show the check box in the exception message box


  • 18th May, 2007: First release
    This is my first article on The Code Project. I will be modifying the document later on.

About Shiras

Currently I am working as a Senior Software Engineer at Proteans Software Solutions in Bangalore.


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


About the Author

Padoor Shiras
Web Developer Proteans Software Solutions Pvt Ltd.
India India
Shiras AbdulRahman Currently working with Proteans Software Solutions Bangalore.

Proteans a CAMO group company is an outsourcing company focusing on software product development and business application development on Microsoft Technology Platform. "Committed to consistently deliver high-quality software products and services through continual improvement of our knowledge and practices focused on increased customer satisfaction.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Member 127476809-Jun-17 2:44
memberMember 127476809-Jun-17 2:44 
GeneralMicrosoft.SqlServer.ExceptionMessageBox under .NET 3.0/3.5 Pin
Craigology25-Nov-07 15:43
memberCraigology25-Nov-07 15:43 
GeneralLocalize ExceptionMessageBox Pin
Olka7414-Oct-07 23:40
memberOlka7414-Oct-07 23:40 
GeneralToo much information Pin
Mark Nischalke18-May-07 7:46
mvpMark Nischalke18-May-07 7:46 
GeneralRe: Too much information Pin
elektrowolf24-Oct-08 11:36
memberelektrowolf24-Oct-08 11:36 
QuestionThanks but what does this show us that the MSDN documentation does not? Pin
stensones18-May-07 3:57
memberstensones18-May-07 3:57 

This is usefull information but, uless I am missing a subtle point, does not seem to add anything to the MSDN srtile at this URL:

Perhaps you would consider adding some value to it by providing a component that not only displays the excpetions it is passed but logs them to the EventLog for later analysis?

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171114.1 | Last Updated 18 May 2007
Article Copyright 2007 by Padoor Shiras
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid