none
为什么我的try-catch语句不工作?(C++) RRS feed

  • 问题

  • 见代码:(在一个事件函数里面,U1是TextBox控件,U1Output是TextBlock控件,ME1是MediaElement控件)

    含义是将U1中输入的URI提供给ME1播放,其中如果出错,尝试在U1前面添加"http://"。

    	try{
    		ME1->Source = ref new Uri(U1->Text);//第二行
    		U1Output->Text = "DONE";
    	}
    	catch(int a){//第五行
    		try{
    			U1->Text = "http://" + U1->Text;
    			ME1->Source = ref new Uri(U1->Text);
    			U1Output->Text = "DONE";
    		}
    		catch (int b){
    			U1Output->Text = "ERROR";
    			return;
    		}
    	}

    但是在运行时,如果我输入一个没有"http://"的语句,直接在第二行报错,原因是U1不是标准URI,根本没有执行第五行的语句。

    (UriBuilder类C++似乎访问不到,所以暂时不要尝试使用UriBuilder解决。)

    请问怎么办?为什么没有执行第五行?

    2014年5月26日 13:27

答案

  • Hi lxylxy123456,

    我这里测试了一下你的代码(稍微改动了一点),貌似没有问题啊,你试试看,我估计是你catch的函数不对,catch括号里面一般写的是Exception的种类,你写了个int,我这里用了三个点,代表捕获所有的异常,其实应该用InvalidArgumentException更为合适:

            auto ME1 = ref new MediaElement();
    	auto U1Output = ref new TextBox();
    	auto U1 = ref new TextBox();
    
    	U1->Text = "www.microsoft.com";
    
    	try
    	{
    		Uri^ uri = ref new Uri(U1->Text);
    		ME1->Source = uri;
    		U1Output->Text = "DONE";
    	}
    	catch (...)
    	{
    		try{
    			U1->Text = "http://" + U1->Text;
    			ME1->Source = ref new Uri(U1->Text);
    			U1Output->Text = "DONE";
    		}
    		catch (...){
    			U1Output->Text = "ERROR";
    			return;
    		}
    	}

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2014年5月27日 2:57
    版主
  • 代码中直接写 InvalidArgumentException^ e 即可。

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2014年5月27日 5:11
    版主
  • 请把那个勾去掉再试试。


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2014年5月27日 9:13
    版主

全部回复

  • Hi lxylxy123456,

    我这里测试了一下你的代码(稍微改动了一点),貌似没有问题啊,你试试看,我估计是你catch的函数不对,catch括号里面一般写的是Exception的种类,你写了个int,我这里用了三个点,代表捕获所有的异常,其实应该用InvalidArgumentException更为合适:

            auto ME1 = ref new MediaElement();
    	auto U1Output = ref new TextBox();
    	auto U1 = ref new TextBox();
    
    	U1->Text = "www.microsoft.com";
    
    	try
    	{
    		Uri^ uri = ref new Uri(U1->Text);
    		ME1->Source = uri;
    		U1Output->Text = "DONE";
    	}
    	catch (...)
    	{
    		try{
    			U1->Text = "http://" + U1->Text;
    			ME1->Source = ref new Uri(U1->Text);
    			U1Output->Text = "DONE";
    		}
    		catch (...){
    			U1Output->Text = "ERROR";
    			return;
    		}
    	}

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2014年5月27日 2:57
    版主
  • InvalidArgumentException是什么?

    按照你的建议,catch括号里面的"..."应该是什么?

    2014年5月27日 4:04
  • 代码中直接写 InvalidArgumentException^ e 即可。

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2014年5月27日 5:11
    版主
  • 还是没有接收到,是不是在调用出错的函数时应该写一些什么?
    2014年5月27日 6:26
  • 你好,我这里代码无误啊~

    你能截个图让我看看为什么代码跳不到Catch中么?

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2014年5月27日 6:33
    版主
  • 请把那个勾去掉再试试。


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2014年5月27日 9:13
    版主
  • 成功了,是不是即使catch了也会提示错误?
    2014年5月27日 9:23
  • 这个可能属于Visual Studio的设置吧。


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2014年5月27日 9:29
    版主