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 }
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 }
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 }