Exception/MessageBox in Calibur.Micro

Dec 20, 2010 at 12:58 PM

Hi guys, I start learning Caliburn.Micro and I am little confuse of handling with exception/messange box in view model class.

I found some blogs about, for example:

http://frankmao.com/2010/11/18/handling-messagebox-in-caliburn-micro/

For example some method in view model class which can produce exception.

 public void MethodWichCanProduceEx(string arg1, string arg2 )
    {
        if(arg1==null)
            throw new ArgumentNullException("arg1 is null");
        if (arg2 == null)
            throw new ArgumentNullException("arg2 is null");

        try
        {

        }
        catch (Exception exception)
        {

            throw exception;
            //? show message box MessageBox.Shox(exception.Message)
        }
    }

What is correct handling and showing these exception in view ? It exist any kind of pattern for caliburn.micro?

It possible trace exception as in .NET in text, xml file ?

For example I would like trace exception in xml, text file and in view show only message.box or something message.

Thank for advance, maybe is my question little stupid, sorry  I am only learning calibur.micro.

Dec 20, 2010 at 5:19 PM

If you are searching for a general exception handler, with a scope a bit more narrow than the general handler provided by the Application.UnhandledException, you may have a look at the filters recipe (http://caliburnmicro.codeplex.com/wikipage?title=Action%20Filters&referringTitle=Documentation), particularly to RescueAttribute.

You can also handle a *specific* exception in the same method where it was generated; this way you can usually take the most appropriate action to rescue from exception. 

That being said, you can accomplish the actual exception handling in VM just like any other C# class.
You can also take advantage of Caliburn integrated error logging (LogManager class).

Displaying a message for the user is also OK; you just have to avoid (out of "stylistic" concerns) to directly use MessageBox class, because it will couple your VM to a specific UI technology, making unit test a little hard.
You might use the same approach of the post you pointed out; my personal preference is to use an explicitly named delegate instead of an anonymous Func<string, string, bool>, or a small IMessageBoxService interface.
If you need to show a more complex UI (for ex. a styled window with an expandable section containing technical exception details) you may consider creating a specific VM+View for this, and display it through IWindowManager.ShowDialog method.

Dec 20, 2010 at 8:23 PM
Edited Dec 20, 2010 at 8:25 PM

Thank you for advices, I used LongManager class and also log4net on logging exception. I have littlle problem with message box, so I create temporary solution with IMessageBox interface in view model class and I inject this class - StandardMessageBox. In finnaly solution I create own UI on showing exception.


public class StandardMessageBox : IMessageBox
{
public void ShowException(Exception exc)
{
MessageBox.Show(ConvertExceptionToString(exc), "Error Occurred");
}
}


Thank you for the valuable advices, it helps me a lot. I used 2 years winforms but wpf with caliburn are great.