.class public Thrower {
.method static public void Start( ) {
ldstr "Oops"
throw
ret
}
}
-----------------------------------------------------------------
在.Net2.0当中,为了确保跨语言的兼容性,CLR会自动将不是继承自System.Exception的异常包裹在RuntimeWrappedException对象中;为了保证和1.x版本的兼容性,.Net 2.0提供了RuntimeCompatibilityAttribute类,指定CLR不要对异常进行包装:[assembly:System.Runtime.CompilerServices.RuntimeCompatibility(WrapNonExceptionThrows = false)]
测试用代码(运行于.Net 2.0):
.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
}
}
如果把第一行的属性去掉,编译时将出现下面的警告:
warning CS1058: A previous catch clause already catches all exceptions. All non-exceptions thrown will be wrapped in a System.Runtime.CompilerServices.RuntimeWrappedException