结构化异常处理是测试特定的代码片段,并在发生异常时不再通过错误列表提示错误,而是在命令提示符中提示错误。它可以发现可预知及不可预知的问题,及允许开发者识别、查出和修改错漏之处。
在C#中使用try/catch/finally语句块处理异常,实例1:
View Code
using System;
using System.Collections.Generic;
using System.Text;
namespace 练习
{
class Program
{
static void Main( string[] args)
{
int a = 0;
int b = 10;
int c = 0;
try
{
a = b / c; // 可能产生异常的代码块
}
catch (Exception e)
{
Console.WriteLine( " 发生异常:{0} ", e.Message); // 对异常进行处理的代码块
}
finally
{
Console.ReadLine(); // 最终执行的代码块
}
}
}
}
using System.Collections.Generic;
using System.Text;
namespace 练习
{
class Program
{
static void Main( string[] args)
{
int a = 0;
int b = 10;
int c = 0;
try
{
a = b / c; // 可能产生异常的代码块
}
catch (Exception e)
{
Console.WriteLine( " 发生异常:{0} ", e.Message); // 对异常进行处理的代码块
}
finally
{
Console.ReadLine(); // 最终执行的代码块
}
}
}
}
运行结果如下:
实例2:使用多个catch块
View Code
using System;
using System.Collections.Generic;
using System.Text;
namespace 练习
{
class Program
{
static void ProcessString( string str)
{
if (str == null)
{
throw new ArgumentNullException();
}
}
static void Main()
{
Console.WriteLine( " 输出结果为: ");
try
{
string str = null;
ProcessString(str);
}
catch (ArgumentNullException e)
{
Console.WriteLine( " 第一个异常:{0} ", e.Message);
}
catch (Exception e)
{
Console.WriteLine( " 第二个异常:{0} ", e.Message);
}
finally
{
Console.ReadLine();
}
}
}
}
using System.Collections.Generic;
using System.Text;
namespace 练习
{
class Program
{
static void ProcessString( string str)
{
if (str == null)
{
throw new ArgumentNullException();
}
}
static void Main()
{
Console.WriteLine( " 输出结果为: ");
try
{
string str = null;
ProcessString(str);
}
catch (ArgumentNullException e)
{
Console.WriteLine( " 第一个异常:{0} ", e.Message);
}
catch (Exception e)
{
Console.WriteLine( " 第二个异常:{0} ", e.Message);
}
finally
{
Console.ReadLine();
}
}
}
}
运行结果为:
Exception异常类是所有异常的基类,异常类可以描述错误的可读文本,发生异常时调用堆栈的状态。在程序发生异常时,想要捕获被抛出的异常,只有该异常类型与某个catch语句中指定的异常类型相匹配时,才会执行catch语句。实例3:
View Code
using System;
using System.Collections.Generic;
using System.Text;
namespace 练习
{
class Program
{
public void test( string s)
{
if (s == null)
throw ( new ArgumentNullException());
}
public static void Main()
{
Program x = new Program();
try
{
string s = null;
x.test(s);
}
catch (ArgumentNullException e)
{
Console.WriteLine( " {0} First exception caught ", e);
}
catch (Exception e)
{
Console.WriteLine( " {0} Exception caught ", e);
}
finally
{
Console.ReadLine();
}
}
}
}
using System.Collections.Generic;
using System.Text;
namespace 练习
{
class Program
{
public void test( string s)
{
if (s == null)
throw ( new ArgumentNullException());
}
public static void Main()
{
Program x = new Program();
try
{
string s = null;
x.test(s);
}
catch (ArgumentNullException e)
{
Console.WriteLine( " {0} First exception caught ", e);
}
catch (Exception e)
{
Console.WriteLine( " {0} Exception caught ", e);
}
finally
{
Console.ReadLine();
}
}
}
}
其结果为:
System.Exception类表示在应用程序执行期间发生的错误,由运行的环境抛出,是其他类的基类,ApplicationException类是由用户程序抛出。
自定义异常类继承于ApplicationException类,通过它可以编写一个通用的catch代码块,捕获应用程序定义的任何自定义异常类。
finally块可以确保发生异常时执行所有的清理工作,必须和try/catch一起使用,而且不管是否发生异常,finally块都会执行。