• 异常处理 Exception


    try
                {
                    string url = "http://www.devbg.org/img/";
                    string file = "Logo-BASD.jpg";
                    string myStringWebResource = null;
     
                    
                    myStringWebResource = url + file;
                    webClient.DownloadFile(myStringWebResource, file);
     
                    //to view the image
                    // System.Diagnostics.Process.Start("Logo-BASD.jpg");
                }
                catch (FileNotFoundException fe)
                {
                    Console.WriteLine(fe.Message);
                }
                catch (System.Net.WebException we)
                {
                    Console.WriteLine(we.Message);
                }
                catch (NotSupportedException ne)
                {
                    Console.WriteLine(ne.Message);
                }
                finally
                {
                    webClient.Dispose();
                    GC.Collect();
                }
     
    ***********
    在C#中所有的异常类型都继承自System.Exception,也就是说,System.Exception是所有异常类的基类. 总起来说,其派生类分为两种:
       1. SystemException类: 所有的CLR提供的异常类型都是由SystemException派生。
       2. ApplicationException类: 由用户程序引发,用于派生自定义的异常类型,一般不直接进行实例化。

       创建自定义异常类应严格遵循几个原则
    1. 声明可序列化(用于进行系列化,当然如果你不需要序列化。那么可以不声明为可序列化的)
    2. 添加一个默认的构造函数
    3. 添加包含message的构造函数
    4. 添加一个包含message,及内部异常类型参数的构造函数
    5. 添加一个序列化信息相关参数的构造函数.

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5. using System.IO;  
    6. using System.Runtime.Serialization.Formatters.Binary;  
    7. namespace ConsoleApplication3  
    8. {  
    9.     [Serializable] //声明为可序列化的 因为要写入文件中  
    10.     public class PayOverflowException : ApplicationException//由用户程序引发,用于派生自定义的异常类型  
    11.     {  
    12.         /// <summary>  
    13.         /// 默认构造函数  
    14.         /// </summary>  
    15.         public PayOverflowException() { }  
    16.         public PayOverflowException(string message)  
    17.             : base(message) { }  
    18.         public PayOverflowException(string message, Exception inner)  
    19.             : base(message, inner) { }  
    20.         //public PayOverflowException(System.Runtime.Serialization.SerializationInfo info,  
    21.         //    System.Runtime.Serialization.StreamingContext context)  
    22.         //    : base(info, context) { }  
    23.     }  
    24.   
    25.     internal class Employee  
    26.     {  
    27.         public int ID { getset; }  
    28.         public string Name { getset; }  
    29.         /// <summary>  
    30.         /// current pay  
    31.         /// </summary>  
    32.         public int CurrPay { getset; }  
    33.   
    34.         public Employee() { }  
    35.         public Employee(int id, string name, int currpay)  
    36.         {  
    37.             this.ID = id;  
    38.             this.Name = name;  
    39.             this.CurrPay = currpay;  
    40.         }  
    41.   
    42.         /// <summary>  
    43.         /// 定义一个GiveBunus的虚方法以供不同的派生类进行重载  
    44.         /// </summary>  
    45.         /// <param name="amount">奖金额度</param>  
    46.         public virtual void GiveBunus(int amount)  
    47.         {  
    48.             //用一个临时变量记录递增之前的值  
    49.             var pay = CurrPay;  
    50.   
    51.             this.CurrPay += amount;  
    52.   
    53.             if (CurrPay > 10000)  
    54.             {  
    55.                 //发生异常,将CurrPay的值进行恢复,  
    56.                 //并抛出异常,外部程序捕获次异常  
    57.                 this.CurrPay = pay;  
    58.                 var ex = new PayOverflowException("The employee's max pay should be no more than 10000.");  
    59.                 throw ex;  
    60.             }  
    61.         }  
    62.     }  
    63.   
    64.     class Program  
    65.     {  
    66.         static void Main(string[] args)  
    67.         {  
    68.             Console.WriteLine("**** 创建Employee对象,并用try/catch捕获异常 *****");  
    69.   
    70.             var emp = new Employee(10001, "Yilly", 8000);  
    71.             try  
    72.             {  
    73.                 emp.GiveBunus(3000);  
    74.             }  
    75.             catch (PayOverflowException ex)  
    76.             {  
    77.                 Console.WriteLine("异常信息:{0} 发生于{1}类的{2}方法", ex.Message,  
    78.                     ex.TargetSite.DeclaringType, ex.TargetSite.Name);  
    79.   
    80.                 try  
    81.                 {  
    82.                     var file = new FileStream(@"c:customerexception.txt", FileMode.Create);  
    83.                     //*** 异常信息写入文件中的代码省略...  
    84.                     //以序列化方式写入  
    85.                     BinaryFormatter bf = new BinaryFormatter();  
    86.                     bf.Serialize(file, ex);  
    87.                     file.Close();  
    88.   
    89.                     //以字节方式写入  
    90.                     //byte[] buffer = System.Text.Encoding.Default.GetBytes(ex.Message);  
    91.                     //int leng = 0;  
    92.                     //leng = buffer.GetLength(0);  
    93.                     //file.Write(buffer, 0, leng);  
    94.                     //file.Close();  
    95.                 }  
    96.                 catch (Exception ex1)  
    97.                 {  
    98.                     var inner = new PayOverflowException(ex.Message, ex1);  
    99.                     throw inner;  
    100.                 }  
    101.             }  
    102.   
    103.         }  
    104.     }  
    105. }  
     值得注意的是:在实例化的时候调用的是PayOverflowException(string message, Exception inner)构造函数,
     如果本程序如果有其他程序在调用的时候, 可以通过.InnerExcetpion的Message属性进行查看内部异常。
  • 相关阅读:
    UPC-5930 Rest Stops(水题)
    UPC-6199 LCYZ的道路(贪心)
    UPC-6198 JL的智力大冲浪(简单贪心)
    POJ 3279 Filptile dfs
    hrbust 1621 迷宫问题II 广搜
    HDU 1045 dfs + 回溯
    优先队列基本用法
    树。森林。和二叉树之间的转换
    POJ 2689 筛法求素数
    哈理工OJ 1328
  • 原文地址:https://www.cnblogs.com/zeroone/p/3308152.html
Copyright © 2020-2023  润新知