你这个异常是有触发,但是被吃掉了
因为 using语句其实是 try...finally 的简写,你的代码相当于
try{
test t=new test();
throw new ArgumentNullException();
} finally {
if(t!=null)t.Dispose();
}
可见异常是有,但已经被using给吃掉了.如果要捕捉异常,可以考虑去掉using改用try,或在内部嵌套try
异常的产生是要被捕获的,所以说针对test有异常的情况,你只能在using前加入try。
其实只是一个问题有异常要捕获,关键是哪一个环节处理异常。
class Program
{
static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
// 处理未捕获
}
}
这是控制台应用程序捕获未捕获的异常,其他程序类似。但是Asp.net可以在Global.ascx文件中直接捕获未捕获的异常。请区分对待。
接分~
try
{}
catch
{}
使用try catch来捕获异常 有异常的时候就抛出异常