Fork me on GitHub
Blog
The journal that this archive was targeting has been deleted. Please update your configuration.
« Report a Message using a ViewModel | Main | Define Behavior Context Objects »
Friday
Oct122012

Catch and Report Behavior Exceptions

All Behaviors execution are isolated piece of methods that are wrapped with a Behavior class. The behaviors have no knowledge of how to report errors and they would just propagate the error (Exception) to the caller up the stack. Now, we want to catch errors whenever a behavior is executed and report it back to the user. To catch exceptions you would override OnBehaviorEvent from the controller and wrap it with a try..catch statement like this:
public partial class Controller
{
        // override OnBehaviorEvent from the Controller class
    protected override void OnBehaviorEvent<T>(
        IExecutableBehavior<T> executableBehavior, 
        BehaviorEvent<T> behaviorEvent)
    {
        try
        {
            base.OnBehaviorEvent<T>(
             executableBehavior, behaviorEvent);
        }
        catch(Exception ex)
        {
            // handle your exception here, in this case
            //..I am using Telerik RadToolTip to display messages
            //..for more information how to use RadToolTip visit: 
            //http://www.telerik.com/products/aspnet-ajax/tooltip.aspx
            this.RadToolTipMessage.Text = ex.Message;
            this.RadToolTipMessage.Title = "Error";
            this.RadToolTipMessage.Visible = true;
            this.RadToolTipMessage.Animation = ToolTipAnimation.Fade;
            this.RadToolTipMessage.Show();
        }
    }

    //..Initialize my RadToolTip
    protected override void OnInit(EventArgs e)
    {
        this.RadToolTipMessage = new RadToolTip()
        {
            ID = "RadToolTipMessage",
            Skin = "Metro",
            VisibleOnPageLoad = true,
            RelativeTo = ToolTipRelativeDisplay.BrowserWindow,
            Visible = false,
            Position = ToolTipPosition.Center,
            HideEvent = ToolTipHideEvent.ManualClose,
            ShowDelay = 15000,
            AutoCloseDelay = 10000,
            HideDelay = 15000
        };

        base.OnInit(e);
    }
    //adding my RadToolTip in my controller
    protected override void CreateChildControls()
    { 
        this.Controls.Add(this.RadToolTipMessage);

        base.CreateChildControls();
        base.EnsureChildControls();
    }
}

References (1)

References allow you to track sources for this article, as well as articles that were written in response to this article.

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>