惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
Help Net Security
Help Net Security
P
Privacy International News Feed
T
Threat Research - Cisco Blogs
C
Cisco Blogs
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
L
LINUX DO - 热门话题
Security Latest
Security Latest
A
Arctic Wolf
G
GRAHAM CLULEY
月光博客
月光博客
S
Securelist
D
Docker
J
Java Code Geeks
T
Troy Hunt's Blog
T
Tenable Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
SecWiki News
SecWiki News
S
Security @ Cisco Blogs
量子位
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
LINUX DO - 最新话题
Recent Commits to openclaw:main
Recent Commits to openclaw:main
aimingoo的专栏
aimingoo的专栏
博客园 - 【当耐特】
H
Heimdal Security Blog
The Hacker News
The Hacker News
博客园 - 三生石上(FineUI控件)
Application and Cybersecurity Blog
Application and Cybersecurity Blog
N
Netflix TechBlog - Medium
Vercel News
Vercel News
Forbes - Security
Forbes - Security
B
Blog RSS Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
IT之家
IT之家
B
Blog
MongoDB | Blog
MongoDB | Blog
博客园 - 聂微东
Google DeepMind News
Google DeepMind News
S
Secure Thoughts
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Check Point Blog
云风的 BLOG
云风的 BLOG
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
The Blog of Author Tim Ferriss
L
Lohrmann on Cybersecurity
F
Full Disclosure
D
Darknet – Hacking Tools, Hacker News & Cyber Security
P
Proofpoint News Feed

博客园 - smalldust

NTT大规模网络故障 亲手焙制一个极其简单但却极其实用的Reflector插件 关于园子里讨论的软件的追求的杂谈 Reflector保护方法初探 .Net 2.0 原汁原味读取注册表 为什么我不用IE7和FireFox 给.Net程序员的PInvoke Tips [2]: Are Strings Immutable? 给.Net程序员的PInvoke Tips [1]: String is Sometimes an Integer CLR Team程序员出的难题,有兴趣的朋友不妨挑战一下 - smalldust - 博客园 Google Trends发布 也谈用反射实现Enum→String映射:一种重视性能的方法 针对个例的、社区性的维基系统设想(草稿) 数学的思考方式 VS 程序的思考方式 WinForm程序启动时不显示主窗体的实现方法 C#程序模拟鼠标操作 [Simulate Mouse Movement and Click Programmatically] 如何在C#中获取“当前目录” Windows Vista将推迟到2007年1月发布 .Net 2.0实例学习:WebBrowser页面与WinForm交互技巧 使用关键字作为自定义标识符
除了Exception,你还能throw什么?
smalldust · 2006-06-21 · via 博客园 - smalldust

用惯了C#,VB.Net的人,可能很习惯用下面的格式来捕获所有的异常:

try {
    
//Some code
}
catch (System.Exception ex) {
    System.Console.Write(
"Error!");
}

这条语句能捕获所有种类的异常吗?显然,这条语句捕获的是System.Exception,以及所有继承自它的类。
也就是说,如果你抛出了一个不是继承自System.Exception的对象,该语句就无法捕获。

抛出不是异常的异常……这种不兼容CLS的事情,可能吗?
答案是,在1.x当中是可能的。

在C++当中,我们可以用 throw "Error!" 这样的语句抛出一个字符串;
在IL当中,我们可以用下面的方式,抛出任意形式的异常:

.assembly ThrowerLib { }

.

class public Thrower {
    .method 
static public void Start( ) {
        ldstr 
"Oops"
        
throw
        ret
    }
}

所以,在.Net 1.x当中,经常使用下面这种最保险的方式:

try
{
    
//Some code
}
catch(System.Exception ex)
{
    System.Console.WriteLine(
"System.Exception error: " + ex.Message);
}
catch
{
    System.Console.WriteLine(
"Non System.Exception based error.);
}

但是,在.Net2.0当中,为了确保跨语言的兼容性,CLR会自动将不是继承自System.Exception的异常包裹在RuntimeWrappedException对象中;这样的结果就是,本文中第一个例子中的代码可以捕获所有类型的异常了。

同时,为了保证和1.x版本的兼容性,.Net 2.0提供了RuntimeCompatibilityAttribute类,指定CLR不要对异常进行包装:

[assembly:System.Runtime.CompilerServices.RuntimeCompatibility(WrapNonExceptionThrows = false)]

附:测试用代码(运行于.Net 2.0)

1,抛出字符串异常的IL代码,用ilasm /DLL编译

// ThrowerLib.il
.assembly ThrowerLib { }

.

class public Thrower {
    .method 
static public void ThrowException( ) {
        ldstr 
"ThrowException exception from the IL world!"
        newobj instance 
void [mscorlib]System.Exception::.ctor(string)
        
throw
        ret
    }

    .method 

static public void ThrowString( ) {
        ldstr 
"Weird exception!"
        
throw
        ret
    }
}

2,测试用C#代码,要添加对上面的DLL的引用

[assembly: System.Runtime.CompilerServices.RuntimeCompatibility(WrapNonExceptionThrows = false)]namespace ThrowerExample
{
    
class ThrowerHarness
    {
        
static void Main(string[] args)
        {
            
try
            {
                Thrower.ThrowException();
            }
            
catch (System.Exception ex)
            {
                System.Console.WriteLine(
"System.Exception error: " + ex.Message);
            }
            
catch 
            {
                System.Console.WriteLine(
"Non System.Exception based error.");
            }
try
            {
                Thrower.ThrowString();
            }
            
catch (System.Exception ex)
            {
                System.Console.WriteLine(
"System.Exception error: " + ex.Message);
            }
            
catch
            {
                System.Console.WriteLine(
"Non System.Exception based error.");
            }
        }
    }
}

执行结果是,第一个异常将被catch(System.Exception ex){}捕获;第二个异常由于catch(System.Exception ex){}无法捕获,将落到catch{}中。

如果把第一行的属性去掉,编译时将出现下面的警告:
warning CS1058: A previous catch clause already catches all exceptions. All non-exceptions thrown will be wrapped in a System.Runtime.CompilerServices.RuntimeWrappedException

继续执行的话,两个异常都将被catch(System.Exception ex){}捕获,不会有任何异常落到catch{}当中