1、try、catch、finally语句
将有可能发生异常错误的代码作为try语句块(即把有可能出现错误的代码放在try语句块内);处理try语句中出现的异常代码放到catch语句块中;finally语句则是不管try语句中有没有异常发生最后都要执行finally语句中的程序块。
例:try-catch-finally语句示例。
using System;
using System.Collections;
namespace 笔记
{
class WithFinally
{
public static void MMain()
{
//有可能发生异常的语句放入到try语句中
try
{
int x = 5;
int y = 0;
int z = x / y; //异常,除数为0
Console.WriteLine(z); //不再执行
}
//try语句发生异常将跳转到catch块,由catch语句块内的程序处理异常
catch (DivideByZeroException)
{
Console.WriteLine("Error occurred,unable to computer");
}
//不管有没有异常发生,都将执行finally语句块中的代码
finally
{
Console.WriteLine("Thank you for using the Program");
}
}
}
}
程序运行结果:
Error occurred,unable to computer
Thank you for using the Program
代码有一个除以零的式子将引发DivideByZeroException异常。发生异常以后,try语句块发生异常语句后面的代码将不再执行,而是寻找与此try语句相关联的catch语句块,并执行其中的代码。finally语句块则不管上面有没有异常发生都要执行。try语句有三种形式:try-catch、try-catch-finally、try-finally。
通常情况下要将可能发生异常的代码放入到try语句块中。一个try块必须有至少一个与之相关联的catch语句块或finally语句块,单独一个try语句块是没有意义的。
catch语句块中包含的是出现异常时要执行的代码。一个try后面可以有零个以上的catch语句块。如果try语句块中没有异常,则catch语句块中的代码不会被执行。catch后面括号放入希望捕获的异常,如上例中DivideByZeroException异常。当两个catch语句的异常类有派生关系的时候,要将包括派生的异常类catch语句放到前面,包括基类的catch语句放置到后面。
finally语句块包含了一定要执行的代码,通常是一些资源释放,关闭文件等代码。
多catch语句示例:
using System;
using System.Collections;
namespace 笔记
{
class WithFinally
{
public static void MMain()
{
try
{
int x = 5;
int y = 0;
int z = x / y; //异常,除数为0
Console.WriteLine(z); //不再执行
}
catch (FormatException)
{
Console.WriteLine("Error occurred,FormatException");
}
catch (DivideByZeroException)
{
Console.WriteLine("Error occurred,DivideByZeroException");
}
catch
{
Console.WriteLine("Error occurred,Exception");
}
finally
{
Console.WriteLine("Thank you for using the program");
}
}
}
}
程序运行结果:
Error occurred,DivideByZeroException
Thank you for using the program
第一个catch语句捕获异常是FormatException,表示参数格式不正确导致的异常,第二个catch语句语句捕获异常是DivideByZeroException,表示用整数类型数据除以零抛出异常,第三个catch语句捕获异常是Exception,这是所有异常类的基类。最终执行第二个catch语句。如果将第三个catch语句作为第一个catch语句,程序编译不能通过,将提示:
“前一个catch子句已经捕获该类型或超类类(“System.Exception”)的所有异常。”
2、throw语句
异常的发生有两种可能:代码执行过程中满足了异常的条件而使程序无法正常运行下去;通过throw语句无条件抛出异常。第一种情况上面已经介绍过了。第二种情况则与第一种情况完全相反,通过throw语句主动在程序中抛出异常抛出的异常要用catch语句捕获,否则程序运行将中断。
throw语句用法:
throw expression
throw语句抛出的异常表达式expression必须表示一个System.Exception类型或它的派生类。也可以在throw语句后没有expression表达式,表示将异常再次抛出。
例:throw语句抛出异常。
using System;
using System.Collections;
namespace 笔记
{
class ThrowExample
{
public void Div()
{
try
{
int x = 5;
int y = 0;
int z = x / y;
Console.WriteLine(z);
}
catch (DivideByZeroException e)
{
throw new ArithmeticException("被除数为零", e); //抛出另一个异常
}
}
public static void MMain()
{
try
{
ThrowExample ThrowException = new ThrowExample();
ThrowException.Div();
}
catch(Exception e) //捕获throw抛出的异常
{
Console.WriteLine("Exception:{0}", e.Message); //输出描述异常的信息
}
}
}
}
程序运行结果:
Exception:被除数为零
throw语句重新抛出一个新的异常ArithmeticException,然后由MMain()中的catch捕获。
例:throw语句异常再次抛出
using System;
using System.Collections;
namespace 笔记
{
class ThrowExample
{
public void Div()
{
try
{
int x = 5;
int y = 0;
int z = x / y;
Console.WriteLine(z);
}
catch (DivideByZeroException)
{
throw;
}
}
public static void MMain()
{
try
{
ThrowExample ThrowException = new ThrowExample();
ThrowException.Div();
}
catch(DivideByZeroException e) //捕获throw再次抛出的DivideByZeroException异常
{
Console.WriteLine("Exception:{0}", e.Message); //输出描述异常的信息
}
}
}
}
程序运行结果:
Exception:试图除以零。