如何扔那不从Exception继承一个例外? [英] How to throw a exception that doesnt inherit from Exception?

查看:98
本文介绍了如何扔那不从Exception继承一个例外?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

解决方案

使用CLR 2.0这个开始是不是你需要担心的情​​景。在CLR现在将自动换行不从System.Exception派生类型的新的异常 RuntimeWrappedException 所有异常(<一href="http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.runtimewrappedexception.aspx">Documentation).

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

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

这篇关于如何扔那不从Exception继承一个例外?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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