如何抛出一个从Exception继承的异常? [英] How to throw a exception that doesnt inherit from Exception?

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

问题描述

我想在下面的代码的空catch块中测试一些异常处理逻辑。

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
}


推荐答案

从CLR 2.0开始,这不是一个场景你需要担心。现在,CLR将自动将所有不会从System.Exception派生的异常包装为一个新的Exception类型 RuntimeWrappedException 文档)。

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天全站免登陆