How to catch each module's exception in my shell app in Prism 4.1?

Topics: Prism v4 - WPF 4
Jul 10, 2013 at 12:12 PM
Hi All,

I am using Prism 4.1 and MVVM pattern in my app. What i want is to create a global class in my shell app to catch each module's exception when that occurs. Any exception in each module is not handled in each module thru "try...catch.." statement.

I am currently using DispatcherUnhandledException in my app.xaml.cs file, code snippet looks like this:
public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        this.DispatcherUnhandledException+=new DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
}
}
    private void App_DispatcherUnhandledException(object sender,
       DispatcherUnhandledExceptionEventArgs e)
{
}

Howerver, it seems to me that the App_DispatcherUnhandledException method can't catch all exception that happens in each module. yes, it did catch some of exceptions, but not all. for example, when an exception happens in the viewmodel of one module, the App_DispatcherUnhandledException is not hit in the shell app.

Could anyone thred some lights on this and help me out?

Any input will be much appreciated.
Developer
Jul 10, 2013 at 7:46 PM
Hi,

As far as I know Prism does not provide any functionally to manage unhandled exceptions globally (and it's also listed as a Challenged not Addressed by Prism in the documentation.) Hence, you will need to include your own logic to catch the exception.

As a starting point you can try subscribing to the AppDomain.CurrentDomain.UnhandledException event. Based on my understanding this event should be raised for any unhandled exceptions in your AppDomain, regardless of the thread.

I hope this helps,

Damian Cherubini
http://blogs.southworks.net/dcherubini
Aug 13, 2013 at 12:50 PM
Hi,

Can a complete sample code be provided for handling global exception with the approach of "subscribing to the AppDomain.CurrentDomain.UnhandledException"?

Thanks
Developer
Aug 13, 2013 at 6:26 PM
Hi,

You can find more information about the UnhandledException event, as well as an example on how to use it in its MSDN page:
Regards,

Damian Cherubini
http://blogs.southworks.net/dcherubini
Aug 14, 2013 at 6:37 AM
Following is the code in AppMain. But the unhandled exception within the Prism Module is not caught by this code. Many websites talk about the fact that this code does not work for Prism application, but could not find any solution for the same. Any help on this will of great help.

Thanks

protected override void OnStartup(StartupEventArgs e)
{

if (DEBUG)

        RunInDebugMode();

else

        RunInReleaseMode();

endif

}
    private static void RunInDebugMode()
    {
        Bootstrapper = new Bootstrapper();
        Bootstrapper.Run();
    }

    private static void RunInReleaseMode()
    {
        AppDomain.CurrentDomain.UnhandledException += AppDomainUnhandledException;
        try
        {
            Bootstrapper = new Bootstrapper();
            Bootstrapper.Run();
        }
        catch (Exception ex)
        {
            HandleException(ex);
        }
    }

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

    private static void HandleException(Exception ex)
    {
        if (ex == null) return;

        MessageBox.Show(ex.Message);
        Environment.Exit(1);
    }
Aug 15, 2013 at 5:18 PM
Hi,

Based on the code snippets you provided, the only thing that looks odd is that you are not defining your new Bootstrapper correctly, since you are using the type as a variable, so you should define a name for your new Bootstrapper like this:
...
Bootstrapper bootstrapper = new Bootstrapper();
bootstrapper.Run();
...
Nevertheless, assuming that this is a typo mistake of yours, the code seems to be working fine as it looks. When running it in Debug mode, if any unhandled exception arises the application will hang. On the other hand, if running it in Release mode and any unhandled exception arises, a message box showing details of the exception will appear.

I also took the time to create a sample of a simple Prism application using Unity container and the code snippets you provided that works as expected. Feel free to download it from my SkyDrive account:

Hope this helps,

Federico Martinez
http://blogs.southworks.net/fmartinez
Aug 19, 2013 at 11:31 AM
Hi Federico Martinez,

Thanks a lot for the sample. Sample is working fine.