This project has moved. For the latest updates, please go here.

Message.Demo

1. Overview

The Message.Demo module is a demonstration of the message service provided by the Infrastructure. The demonstration contains:
  • Show messages in a modal dialog
  • Update the application status bar
  • Throw a predefined exception to see how the application reacts on unhandled exceptions.

exception-handling.png
Figure 1

Figure 1 shows how the Test Suite reacts on unhandled exceptions. By default the application shows unhandled exceptions through the message service and logs the occurence of the exception via the Logging Application Block.

Note: The exeption handling is activated in Release mode only. Furthermore the application should be started without the debugger because the debugger would interfere with the exception.

2. How to deal with unhandled exceptions?

Listing 1 shows a part of the ShellApplication class. The handling of the AppDomain.CurrentDomain.UnhandledException is already implemented by the Smart Client Software Factory. The same is true for the try catch statement. However it makes sense to listen to the Application.ThreadException event too. This event is raised if an unhandled exception occurs in the UI thread. The exceptions generated with the Message.Demo module are handled through this event.

private static void RunInReleaseMode()
{
    AppDomain.CurrentDomain.UnhandledException += AppDomainUnhandledException;
    Application.ThreadException += ApplicationThreadException;

    try
    {
        new ShellApplication().SplashRun(Resources.Splashscreen);
    }
    catch (Exception ex)
    {
        HandleException(ex, false);
    }
}

private static void AppDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    HandleException(e.ExceptionObject as Exception, e.IsTerminating);
}

private static void ApplicationThreadException(object sender, ThreadExceptionEventArgs e)
{
    HandleException(e.Exception, false);
}

private static void HandleException(Exception ex, bool isTerminating)
{
    if (ex == null) { return; }

    ExceptionPolicy.HandleException(ex, "Default Policy");

    if (!isTerminating)
    {
        MessageForm form = new MessageForm();
        form.MessageType = MessageType.Error;
        form.Message = Resources.UnknownError;
        form.Details = ex.ToString();
        form.ShowDialog();
        form.Dispose();
    }
}

Listing 1

Last edited Oct 8, 2007 at 2:56 PM by jbe2277, version 5

Comments

No comments yet.