.NET Framework Bookmark and Share   
 index > Windows Presentation Foundation (WPF) > How to handle exceptions in the XAML side?
 

How to handle exceptions in the XAML side?

Hi,

I have a listbox which source is by data binding.

In the converter i throw an exception in case of a null incoming object.

How can i catch that exception?

Thx,

Nuno


sinosoidal

you cannot have error handling in XAML what you can have though is something like this

Code Snippet

public Window1()
{
InitializeComponent();

Application.Current.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(Current_DispatcherUnhandledException);
}

void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
//do error handling code here
}


hope it helps


Marlon Grech

you cannot have error handling in XAML what you can have though is something like this

Code Snippet

public Window1()
{
InitializeComponent();

Application.Current.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(Current_DispatcherUnhandledException);
}

void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
//do error handling code here
}


hope it helps


Marlon Grech
How can I do if the Application object does not exists?

In my case I have a XAML UserControl hosted in a Windows Forms application. How can I catch the exceptions I get in my Converters?
  • Proposed As Answer bywarriors89 Monday, September 29, 2008 10:58 PM
  •  
marco.ragogna
Just wondering if anybody has found an answer to the above problem?

I also wish to catch XAML exceptions from my UserControl. They can not be handled correctly from the hosting Windows Forms Application.

Thanks
Jamie
warriors89
If anyone is interested, I found an eventthat is fired for unhandled exceptions, that you can hook into from a UserControl:

this.Dispatcher.UnhandledException+=new
System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(Dispatcher_UnhandledException);

I then threw a custom app exception from Dispatcher_UnhandledException.

Then from my hosting winforms application I was able to catch the custom app exception like so:

DimdomainAsAppDomain=AppDomain.CurrentDomain
AddHandlerdomain.UnhandledException,AddressOfMyUnhandledException

Cheers
Jamie

  • Proposed As Answer bywarriors89 Monday, September 29, 2008 11:06 PM
  •  
warriors89

You can use google to search for other answers

Custom Search

More Threads

• How does one implement a Validation template in C# code?
• Isolated storage for password
• Referencing a Resource file from another library
• What is the equivalent in WPF for a datagridview?
• Dynamic Context Menu with HierarchicalDataTemplate in code behind
• html content & frame
• DependencyProperty question
• How to set the size of the Thumb?
• public code to render a mobius strip?
• convert Flow document to pdf file?