Try-Catch unknown behaviour
I'm currently learning after a book about how to convert xaml code into objects during runtime.
I have the following code:
try
{
ctrl = XamlReader.Load(xaml) as UserControl;
}
catch (Exception exc)
{
OnXamlResult(new XamlCruncherEventArgs(exc.Message));开发者_运维知识库 return;
}
The code is not mine, I took it from the book.
The problem is that try-catch does not work properly, or at least as I know till now. During debugging the program stops when it reaches the line :
ctrl = XamlReader.Load(xaml) as UserControl;
without catching the exception.
What am I doing wrong or how can I solve this problem?
"xaml" is a string. It is taken from a textbox and if the xaml is correctly typed by the user the program should convert the xaml code into object otherwise it should display the corresponding error.
About how the program reacts, i can say that debug stops(it returns me to visual studio pointing the error) and it is not behaving like going into a infinite cycle.
It's probably the debugger breaking as the exception occurs. Stick a breakpoint inside the catch and F5 to continue, it should carry on and hit your breakpoint.
And by "breaking", I don't mean it fails, I mean it pauses execution of the app on the offending line of the exception, so it's a Good Thing in this instance.
If you are using Visual Studio, you can enable/disable this "break on exception" behaviour:
- Debug -> Exceptions... (Ctrl + Alt + E)
- Common Language Runtime Exceptions, check the boxes to the right as needed.
As for the exception itself, unless it's there specifically to show exceptions, it is likely having trouble loading the provided XAML string.
精彩评论