try
{
    //Do something
}
catch(Exception ex)
{
    throw new Exception(ex.Message.ToString());
}
throw new Exception(ex.Message.ToString());时,有个warning:
Warning 92 CA2201 : Microsoft.Usage : 'Test.TestPage()' creates an exception of type 'Exception', an exception type that is not sufficiently specific and should never be raised by user code. If this exception instance might be thrown, use a different exception type.
我想清除这个warning,但不知道怎样清除。请高手们指点一二,谢谢!

解决方案 »

  1.   

    请顺便告知 throw和throw new Exception(ex.Message.ToString())的区别。谢谢
      

  2.   

    http://blog.csdn.net/wygyhm/article/details/2493386
      

  3.   


    try
    {
      //Do something
    }
    catch(Exception ex)
    {
      //直接注销即可,不可不建议这么做,有隐患!
      //throw new Exception(ex.Message.ToString());
    }
      

  4.   

    谢谢,很好的资料,详细的说了throw和throw new..的区别。
      

  5.   

    try{}catch{....}本来就是捕捉异常的,干嘛又抛出异常啊!
    如果你想直接用throw ,也可以:
             if( input % 2 != 0 )      //判断是否被2整除!
                    throw new Exception( String.Format( 
                        "The argument {0} is not divisible by 2.", 
                        input ) );            else return input / 2;
      

  6.   

    try
    {
      //Do something
    }
    catch(Exception)
    {
      throw ;
    }
      

  7.   

    1L的问题2L回答你了。Microsoft.Usage : 'Test.TestPage()' creates an exception of type 'Exception', an exception type that is not sufficiently specific and should never be raised by user code. If this exception instance might be thrown, use a different exception type.这是一个警告。因为 System.Exception 是代表系统异常的(大多由系统库丢出),所以不建议你直接丢出这样的异常,因为别人(调用者)没有办法直接从异常类型直接知道出了什么问题,违背了最佳实践。最好的办法是定义一个
    class TestPageException : Exception
    {
        public TestPageException(string Message) : base(Message) { }
    }
    然后丢出 
    new TestPageException(ex.Message)
      

  8.   

    一般是用MessageBox.show(e.Message)直接输出了事。
    --reply by CSDN Study V1.0.0.3 (starts_2000)
      

  9.   

    一般来说你的调用者会这么处理:
    try
    {}
    catch (DataBaseException ex)
    {
        MessageBox.Show("数据库出错");
    }
    catch (IOException ex)
    {
        MessageBox.Show("写文件出错");
    }
    catch (Exception ex)
    {
        throw ex; // 无法处理,丢给更上层。
    }如果用户代码都丢出 Exception,就给你的调用者造成模糊。所以 C# 编译器就会给你个警告,让你避免这么做。当然,本身这么做也没什么特别严重的后果。
      

  10.   

    base(Message)这个参数从哪来的?
      

  11.   

    一般上层有接受这个Exception时才throw。 没有就直接抛出错误信息好了
      

  12.   

    try
    {
      //容易出错的代码
    }
    catch(Exception ex)
    {
      throw ex;
    }