尝试捕获未知行为 [英] Try-Catch unknown behaviour

查看:50
本文介绍了尝试捕获未知行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我叫亚历克斯.
我目前正在学习一本关于如何在运行时将xaml代码转换为对象的书.

我有以下代码:

Hi, my name is Alex.
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;
           }




该代码不是我的,我是从书中摘下来的.

问题是try-catch无法正常运行,或者至少到目前为止我还不知道.
在调试过程中,程序到达以下行时停止:




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;


没有捕捉到异常.

我在做错什么或如何解决此问题?

提前谢谢!


很抱歉错过了一些东西.
xaml是一个字符串.它来自文本框,如果用户正确输入了xaml,则程序应将xaml代码转换为对象,否则应显示相应的错误.

关于程序的反应方式,我可以说调试停止了,它的行为不像进入无限循环.


without catching the exception.

What am I doing wrong or how can I solve this problem?

Thanks in advance!


Sorry for missing some things.
xaml is a string. It is taken form 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 and it is not behaving like going into a infinite cycle.

推荐答案

0)我们不知道xaml变量表示.向我们展示它是如何初始化的.

1)如果您的xaml(XmlReader)参数不为null,则不会获得异常.
0) We don''t know what the xaml variable represents. Show us how it was initialized.

1) If your xaml (XmlReader) parameter isn''t null, you won''t get an exception.


程序停止"是什么意思?它真的停止了吗?
如果程序只是挂起,则它与try/catch块无关:可能是Load操作导致了它(没有抛出).
What do you mean with ''program stops''? Does it really stop?
If the program just hangs then it has nothing to do with the try/catch block: probably the Load operation causes it (without throwing).


没有任何错误,请尝试使用catch块没有达到期望.在您的代码中没有错误.
With Out Any Error Try Catch Block Will Not Catch the Expection.In your Code there is No Error.


这篇关于尝试捕获未知行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆