WPF trick: Debugging the XamlParseException

Update: See more information about XAML debugging here.

Here's a trick: When you work a lot with XAML, you'll be confronted to the dreaded XamlParseException sooner or later. This exception is thrown whenever something goes wrong while the XAML code is parsed. However, this exception is not very helpful. Typically, you get a message box telling you that:
An unhandled exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
Additional information: Cannot create instance of 'Window1' defined in assembly 'GalaSoftLb.Wpf.MyApplication, Version=1.0.2629.15160, Culture=neutral, PublicKeyToken=null'. Exception has been thrown by the target of an invocation. Error in markup file 'Window1.xaml' Line 1 Position 9.
Right... so what exactly went wrong? Well, it's easy to find out: In the code-behind, the XAML code is parsed in the method InitializeComponent which is automatically generated. This method is called in the Window object's constructor. So to have more details about the exception, put the call to InitializeComponent in a try/catch block. This way, you have access to the useless XamlParseException, but also to its InnerExceptions and to the StackTrace.
public partial class Window1 : System.Windows.Window { public Window1() { try { InitializeComponent(); } catch ( Exception ex ) { // Log error (including InnerExceptions!) // Handle exception } } }
Print | posted on Wednesday, March 14, 2007 8:49 AM

Feedback

# re: WPF trick: Debugging the XamlParseException

left by Daniel Puzey at 3/14/2007 11:33 AM Gravatar
There's another trick to this. Often, by default, you'll get an error reported at Line 1 of the xaml, which is an obvious lie. You can catch the original exception, though:

- Open the "Exceptions" window (Debug/Exceptions) in Visual Studio.
- Click "add"
- Add "System.Windows.Markup.XamlParseException"
- Check the box to break on throw for this exception.
- Hit F5!

You'll find that the XamlParseException you catch is much more descriptive, and will give the correct position in the xaml file.

# re: WPF trick: Debugging the XamlParseException

left by Laurent at 3/14/2007 11:41 AM Gravatar
Very cool, thanks for the feedback, Daniel!

# re: WPF trick: Debugging the XamlParseException

left by Patrick Sears at 8/29/2007 3:58 PM Gravatar
I realize this is 5 months after your post -

Another way you can do this is when the IDE breaks into the debugger and shows you the dreaded "Cannot create instance of 'Window1' blah blah" exception, click View Detail, and drill down on the InnerException property until you get to the real exception.

That way, you don't have to add the XamlParseException to the caught exceptions, and you don't have to wrap InitializeComponent() in a try-catch.

This method is essentially the same as catching the XamlParseException (it's exactly the same exception, obviously), except without the extra work.

# re: WPF trick: Debugging the XamlParseException

left by Laurent at 8/30/2007 7:44 AM Gravatar
Hi Patrick,

Yeah you're a lucky guy, doing WPF with VS2008 :-) Back when I wrote this, VS2008 was not really that ready yet, and your solution doesn't apply to VS2005.

VS2008 makes programming and debugging easier, no doubts :-) Thanks for your comment.

Laurent

# re: WPF trick: Debugging the XamlParseException

left by Kurt at 9/19/2007 7:16 AM Gravatar
Actually, I get Patrick's solution on VS2005 v8.0.5. Laurent, can you not update fully and get it?

# re: WPF trick: Debugging the XamlParseException

left by Laurent at 9/20/2007 7:50 AM Gravatar
Hi Kurt,

One of my colleagues was also able to use the "VS2008-like" exception message box in VS2005. Unfortunately, he and I don't know why he gets it, and I don't. I also have VS2005 V8.05

Microsoft Visual Studio 2005, Version 8.0.50727.762 (SP.050727-7600)

What is the exact version number you use?

Greetings,
Laurent

# re: WPF trick: Debugging the XamlParseException

left by Rick at 9/28/2007 7:38 AM Gravatar
Tools->Options->Debugging->General->Check Enable Exception Assistant

# re: WPF trick: Debugging the XamlParseException

left by Laurent at 9/28/2007 7:55 AM Gravatar
Every day I learn new things... Rick, you're my new thing today :-) Thanks

# re: WPF trick: Debugging the XamlParseException

left by Narendra at 9/29/2007 1:27 AM Gravatar
Thanks for bailing me out of crunch situation. Actually i am developing an WPF application in crunch deadline situation..

Ur post helped to figure out problem in the code

# re: WPF trick: Debugging the XamlParseException

left by Bill at 10/20/2007 10:26 AM Gravatar
Hi,

I'm having this same problem - plus the generated method call to InitializeComponet() cannot be found with the error "Cannot resolve symbol InitializeComponent". Everything builds and when I create a new Windows project using the Extentions to VS2005 for WCF/WPF it runs just fine. Just can't resolve the InitializeComponent. I spent 12 hours today uninstalling everything from VS2005 to the runtimes for both .net 2.0 and 3.0 and still have the same problem. Anyone seen this or know what's up?

thanks!
Bill

# re: WPF trick: Debugging the XamlParseException

left by Dino at 3/27/2008 6:43 PM Gravatar
try...catch...That worked!
thanks!

# re: WPF trick: Debugging the XamlParseException

left by TBeaulieu at 4/8/2008 1:43 AM Gravatar
Thank you for this thread!

I spent all day trying to figure out why I couldn't catch the exception when trying to parse xaml with the reader.

At first, I couldn't even find the Debug...Exceptions menu item, so I thought there was a version difference. Eventually I figured out I just needed to customize the menu by adding it.

So... it's the only exception that by default can't be caught, and the only way to make it catchable is hidden! CRAZY!

# re: WPF trick: Debugging the XamlParseException

left by Martin Dahlborg at 6/11/2008 10:14 PM Gravatar
Great info thank you.

I tried the try-catch and found out that the XamlParseException was not thrown in InitializeComponent();

I needed to include my other methods also to find out that I had a mis-match in my App.Config (this is a WCF client).

try
{
InitializeComponent();
ClearScreen();
SetUpWcf();
}
catch...

I have now read the rest of the comments and can remove my try-catch ;)

# re: WPF trick: Debugging the XamlParseException

left by vera at 9/2/2008 3:17 AM Gravatar
thanks... your site realy help me.. =)

# re: WPF trick: Debugging the XamlParseException

left by Dim at 11/20/2008 2:58 PM Gravatar
hello,

I'm sure it's not the good place, but ...
I want to put a try catch for all my program.
in framework 2, I put it in the static void Main, but where can I put it on wpf ?

Thank you by advance

# re: WPF trick: Debugging the XamlParseException

left by Xigga at 11/20/2008 3:58 PM Gravatar
Thanks Patrick! I was getting the same error and the InnerException helped me find it. Now to fix that damn error. :(

# re: WPF trick: Debugging the XamlParseException

left by Laurent at 11/22/2008 5:37 PM Gravatar
Hey Dim,

In WPF, you put the “default error handling”, that is handling errors that are not caught anywhere else in the application, in the App.xaml. Use the event “DispatcherUnhandledException” for this. This event is called when an exception is not handled within the app, just before the application crashes.

Please note that you should not simply handle unhandled exceptions. It is preferable to use this event to log the error, clean up and then “crash gracefully”. If you simply handle unhandled exceptions, you leave the application in an unknown state which might cause even worse side effects later.

Cheers,
Laurent

# re: WPF trick: Debugging the XamlParseException

left by rüya tabiri at 11/25/2008 10:16 AM Gravatar
Thank you...
Title  
Name
Email (never displayed)
Url
Comments   
Please add 8 and 4 and type the answer here: