Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7176585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:32:23+00:00 2026-05-28T16:32:23+00:00

We have several modules in our application. I have a project with a group

  • 0

We have several modules in our application. I have a project with a group of custom user controls which is used in the modules. Note that this user controls are not injected to the modules instead it is used as a user cotrol in the XAML inline.

Now the user controls can throw exceptions when it is loaded. I would like to handle the exceptions at the module level. The idea is I have an eventaggregator injected to the module constructor and upon catching the exception I can fire an error event.

For example the mainview of the module is as follows

XAML

<inf:DockBase 
    x:Class="MainView" 
    xmlns:inf="clr-namespace:Infrastructure"
    xmlns:usercontrols="clr-namespace:UserControls;assembly=Myusercontrollib">

    <Grid>
        <!--some code here-->
        <usercontrols:CustomListControl source={Binding myList}/>
    </Grid>
</inf:DockBase>

Code behind

public MainView()           
{
    InitializeComponent();         
}

public MainView(MainViewViewModel viewmodel, IEventAggregator eventAggregator)
    :this()  
{
    _eventAggregator = eventAggregator;  
}

Where can I catch excpetions from the user control in the module level??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:32:24+00:00Added an answer on May 28, 2026 at 4:32 pm

    Just shooting from the hip. Perhaps the simplest method to catch exceptions on initialization is to allow access to your event aggregator via a singleton and raise the event from the view.

    Once your application has started up however, if you wish to handle unhandled exceptions on the UI thread without the application crashing, you can try the Application.UnhandledException event. This lets you handle an exception in Silverlight or WPF and process it, then either cancel or continue to allow the exception to throw you out of the application.

    Disclaimer: All untested code, both in compilation and execution

    public class MyView : UserControl
    {   
        public MyView()
        {
            // Hardly best practice, but it'll work
            try
            {
                InitializeComponent();
            }
            catch(Exception caught)
            {
                EventAggregatorService.Instance.GetEvent<XamlExceptionEvent>().Publish(/* ... */);
            }
        }
    }
    
    public class EventAggregatorService
    {
        public IEventAggregator Instance { get { return _instance; } } 
    
        // NOTE: Must be called once in your bootstrapper to set the EA instance
        public static void SetEventAggregator(IEventAggregator instance)
        {
            _instance = instance;
        }
    }
    

    Taking it a step further, if you can create a base viewmodel like this

    // Base viewmodel type to handle errors
    public abstract class ErrorViewModel
    {
        private IEventAggregator eventAggregator;
        protected ErrorViewModel(IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;
        }
    
        public void HandleInitializationException(object view, Exception caught)
        {
            // Publish event via _eventAggregator
        }
    }
    

    Where derived view models are defined like this

    // Derived viewmodel type
    public class DerivedViewModel : ErrorViewModel
    {
        public DerivedViewModel (IEventAggregator eventAggregator) : base(eventAggregator)
        {
        }
    }
    

    You could apply the following method to your view as follows, by catching the exception and deferring error handling.

    public class MyView : UserControl
    {   
        private readonly Exception _ex;
    
        public MyView()
        {
            try { InitializeComponent(); } catch (Exception caught) { _ex = caught; } 
        }
    
        protected override OnDataContextChanged(object sender, EventArgs e)
        {
            if (_ex == null) return;
    
            var vm = view.DataContext as ErrorViewModel;
            if (vm != null)
            {
                vm.HandleInitializationException(view, caught);
                return;
            }
    
            throw new Exception("Error occurred during View initialization", _ex);
        }
    }
    

    Ok its not neat and its not pretty, but again it’ll work. Taking it a step further you may be able to create a base view type to factor out initialization too, however if you are inheriting multiple different base types then this won’t help you. Alternatively a help class to defer initialization, calling the Initializecomponent() method via reflection.

    Finally, the code for UnhandledException handling:

    public partial class App : Application
    {
        public App()
        {
            this.UnhandledException += this.Application_UnhandledException;
    
            InitializeComponent();
        }
    
        private void Application_UnhandledException(object sender, 
            ApplicationUnhandledExceptionEventArgs e)
        {
            if (e.ExceptionObject is FileNotFoundException)
            {
                // Inform the user
                EventAggregatorService.Instance.GetEvent<MyUnhandledExceptionEvent>().Publish(/* ... */);
                // Recover from the error
                e.Handled = true;
                return;
        }
    }
    

    Just some ideas. I’d be interested to hear if there is a defacto solution to this as I’ve often encountered this problem!

    Best regards

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have developed a framework that is used by several teams in our organisation.
I'm working on a site, which will have several modules that either fully available
I have maven project that consists of several modules. I want to add an
I have several mini wars that are modules of a larger application running on
I have a spring web application which has several modules. Each module has its
I have have a main project I am working on, which has several modules/directories.
In a maven project I have several modules which only have a persitence.xml for
So I have a spring application divided into several modules, each in a separate
I have a project divided into several sub-modules (each of them are jar libraries):
I have several RequiredFieldValidators in an ASP.NET 1.1 web application that are firing on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.