zoukankan      html  css  js  c++  java
  • 异常(Exception)

      1、IOException

     1 public static void Test1()
     2         {
     3             FileStream fs = null;
     4             try
     5             {
     6                 fs = new FileStream("d:\file.txt", FileMode.Open);
     7                 fs.ReadByte();
     8                 Console.WriteLine("OK");
     9             }
    10             catch (IOException)
    11             {
    12                 Console.WriteLine("IOException");
    13             }
    14             catch
    15             {
    16                 Console.WriteLine("ELSE Exception");
    17             }
    18             finally
    19             {
    20                 if (fs != null)
    21                 {
    22                     fs.Close();
    23                     fs = null;
    24                 }
    25             }
    26         }
    IOException

      2、FileNotFoundException 继承自 IOException

     1         public static void Test1()
     2         {
     3             FileStream fs = null;
     4             try
     5             {
     6                 fs = new FileStream("d:\file.txt", FileMode.Open);
     7                 fs.ReadByte();
     8                 Console.WriteLine("OK");
     9             }
    10             catch (FileNotFoundException)
    11             {
    12                 Console.WriteLine("FileNotFoundException");
    13             }
    14             catch (IOException)
    15             {
    16                 Console.WriteLine("IOException");
    17             }
    18             finally
    19             {
    20                 if (fs != null)
    21                 {
    22                     fs.Close();
    23                     fs = null;
    24                 }
    25             }
    26         }
    FileNotFoundException

      3、NullReferenceException

     1         /// <summary>
     2         /// 委托
     3         /// </summary>
     4         /// <param name="str">委托参数</param>
     5         public delegate void Show(string str);
     6 
     7         /// <summary>
     8         /// 事件
     9         /// </summary>
    10         public static event Show ShowEvent;
    11 
    12         public static void Test2()
    13         {
    14             try
    15             {
    16                 Console.WriteLine(ShowEvent.GetInvocationList().Count());
    17             }
    18             catch (NullReferenceException)
    19             {
    20                 Console.WriteLine("NullReferenceException");
    21             }
    22         }
    NullReferenceException
  • 相关阅读:
    [bzoj3694]最短路
    [bzoj3172][Tjoi2013]单词
    [bzoj2243][SDOI2011]染色
    [bzoj1036][ZJOI2008]树的统计Count
    [学习笔记]树链剖分
    [bzoj4552][Tjoi2016][Heoi2016]排序
    [51nod1515]明辨是非
    [51nod1685]第k大区间
    [日常训练]training
    BZOJ3811: 玛里苟斯
  • 原文地址:https://www.cnblogs.com/Jacob-Wu/p/5779186.html
Copyright © 2011-2022 走看看