-
c#语言try catch异常处理语句
try-catch错误处理表达式允许将任何可能发生异常情形的程序代码放置在try{}程序代码块进行监控,真正处理错误异常的程序代码则被放置在catch{}块里面,一个try{}块可对应多个catch{}块。
示例 try-catch语句写入多个catch的使用
通过两个catch语句进行捕获异常,它们分别是ArgumentNullException异常和Exception异常。程序代码如下。
usingSystem;
classMainClass
{
staticvoidProcessString(stringstr)
{
if(str==null)
{
thrownewArgumentNullException();
}
}
staticvoidMain()
{
Console.WriteLine("输出结果为:");
try
{
stringstr=null;
ProcessString(str);
}
catch(ArgumentNullExceptione)
{
Console.WriteLine("{0}Firstexception.",e.Message);
}
catch(Exceptione)
{
Console.WriteLine("{0}Secondexception.",e.Message);
}
}
}