如何扔那不从Exception继承一个例外?不从、Exception

由网友(我一直在)分享简介:我想测试一些异常处理逻辑的跌破code中的空的catch块。尝试{//做一些东西抛出一个异常//这是code,我需要}赶上(例外){//处理事情,它继承异常}抓住{//处理事情,不从Exception继承//要测试的这款code}解决方案 使用CLR 2.0这个开始是不是你需要担心的情​​景。...

我想测试一些异常处理逻辑的跌破code中的空的catch块。

 尝试
{
//做一些东西抛出一个异常
//这是code,我需要
}
赶上(例外)
{
//处理事情,它继承异常
}
抓住
{
//处理事情,不从Exception继承
//要测试的这款code
}
 

解决方案

使用CLR 2.0这个开始是不是你需要担心的情​​景。在CLR现在将自动换行不从System.Exception派生类型的新的异常 RuntimeWrappedException 所有异常(Documentation).

该包装可以通过启用应用程序兼容性的水平被禁用,但它肯定是不正常的或常见的情况

2021年起,继承父母房产 有变 新规下,独生子女也不例外

I would like to test some exception handling logic in the empty catch block of the below code.

try
{
	//Do Some stuff that throws a exception 
	//This is the code i need
}
catch (Exception)
{
	//Handle things that inherits from Exception
}
catch
{
	//Handle things that dont inherits from Exception
	//Want to test this code
}

解决方案

Starting with CLR 2.0 this is not a scenario that you need to worry about. The CLR will now automatically wrap all exceptions which do not derive from System.Exception with a new Exception of type RuntimeWrappedException (Documentation).

This wrapping can be disabled by enabling a level of application compatibility but it is certainly not the normal or common case

阅读全文

相关推荐

最新文章