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,但不知道怎样清除。请高手们指点一二,谢谢!
{
//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,但不知道怎样清除。请高手们指点一二,谢谢!
try
{
//Do something
}
catch(Exception ex)
{
//直接注销即可,不可不建议这么做,有隐患!
//throw new Exception(ex.Message.ToString());
}
如果你想直接用throw ,也可以:
if( input % 2 != 0 ) //判断是否被2整除!
throw new Exception( String.Format(
"The argument {0} is not divisible by 2.",
input ) ); else return input / 2;
{
//Do something
}
catch(Exception)
{
throw ;
}
class TestPageException : Exception
{
public TestPageException(string Message) : base(Message) { }
}
然后丢出
new TestPageException(ex.Message)
try
{}
catch (DataBaseException ex)
{
MessageBox.Show("数据库出错");
}
catch (IOException ex)
{
MessageBox.Show("写文件出错");
}
catch (Exception ex)
{
throw ex; // 无法处理,丢给更上层。
}如果用户代码都丢出 Exception,就给你的调用者造成模糊。所以 C# 编译器就会给你个警告,让你避免这么做。当然,本身这么做也没什么特别严重的后果。
{
//容易出错的代码
}
catch(Exception ex)
{
throw ex;
}