public void MyFn(string s)
{
if (s == null)
throw(new ArgumentNullException());
}
}
输出
发生以下异常:
System.ArgumentNullException
示例
此例使用了两个 catch 语句。最先出现的最特定的异常被捕获。
// Ordering catch clauses
using System;
class MyClass
{
public static void Main()
{
MyClass x = new MyClass();
try
{
string s = null;
x.MyFn(s);
}
// Most specific:
catch (ArgumentNullException e)
{
Console.WriteLine("{0} First exception caught.", e);
}
// Least specific:
catch (Exception e)
{
Console.WriteLine("{0} Second exception caught.", e);
}
}
public void MyFn(string s)
{
if (s == null)
throw new ArgumentNullException();
}
}
输出
发生以下异常:
System.ArgumentNullException
在前面的示例中,如果从特定程度最小的 catch 子句开始,您将收到此错误信息:
A previous catch clause already catches all exceptions of this or a super type ('System.Exception')
但是,若要捕获特定程度最小的异常,请使用下面的语句替换 throw 语句:
throw new Exception();