捕获或抛出的类型必须派生自System.Exception [英] The type caught or thrown must be derived from System.Exception

查看:583
本文介绍了捕获或抛出的类型必须派生自System.Exception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用异常处理.同时使用以下代码.它显示错误消息,如下所示

Hi

I am using Exception Handling. while using the below code . it shows the error msg as below

try
{

}
catch(Exception ex)
{
   throw ex;
}


错误消息为:

捕获或抛出的类型必须派生自System.Exception

请帮助我解决这个问题.

在此先感谢
Mohan


Error msg is :

The type caught or thrown must be derived from System.Exception

please help me to solve this problem.

Thanks in Advance
Mohan

推荐答案

Mohan,

显示的错误消息非常简单.当"Exception"类的模棱两可的声明时,可能会发生此错误.我建议您在catch块中使用完整的名称空间(将Exception加上System前缀),如下所示.

解决方案:
Hi Mohan,

The Error message displayed is fairly straight forward. This error would happen possibly when there are ambiguous declaration of "Exception" class. I would suggest you to use the complete namespace(prefix the Exception with System) in the catch block as below.

Solution:
try
{

}
catch (System.Exception ex)
{
    throw ex;
}



使用以下脚本重现错误情况:



Use the below script to reproduce the error scenario:

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

    public class Exception
    {

    }
}


似乎您已自定义异常处理:

It seems like you have a custom exception handling, if so:

try
{

}
catch(Exception ex)
{
   throw new System.Exception(ex.ToString()); // you may change to ex.Message if you have a property Message on your custom exception.
}


嗨....尝试使用以下代码:

hi.... try using below code:

try
{

}
catch(System.Exception ex)
{
   throw ex;
}


这篇关于捕获或抛出的类型必须派生自System.Exception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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