结构化异常处理

结构化异常处理是测试特定的代码片段,并在发生异常时不再通过错误列表提示错误,而是在命令提示符中提示错误。它可以发现可预知及不可预知的问题,及允许开发者识别、查出和修改错漏之处。

 在C#中使用try/catch/finally语句块处理异常,实例1:

 

ExpandedBlockStart.gif 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(); // 最终执行的代码块
            }
        }
    }
}

 

 运行结果如下:

实例2:使用多个catch块

ExpandedBlockStart.gif 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();
            }
        }
    }
}

运行结果为:

Exception异常类是所有异常的基类,异常类可以描述错误的可读文本,发生异常时调用堆栈的状态。在程序发生异常时,想要捕获被抛出的异常,只有该异常类型与某个catch语句中指定的异常类型相匹配时,才会执行catch语句。实例3:

ExpandedBlockStart.gif 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();
            }
        }
    }
}

其结果为:

System.Exception类表示在应用程序执行期间发生的错误,由运行的环境抛出,是其他类的基类,ApplicationException类是由用户程序抛出。

 

 自定义异常类继承于ApplicationException类,通过它可以编写一个通用的catch代码块,捕获应用程序定义的任何自定义异常类。

 

 finally块可以确保发生异常时执行所有的清理工作,必须和try/catch一起使用,而且不管是否发生异常,finally块都会执行。

转载于:https://www.cnblogs.com/LSshaojun/archive/2012/02/14/2351821.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值