集合的长度看起来是可变的 ……class
ArrayList与Hashtable(键值对)
异常处理
我们将有可能发生异常的代码用Try 块包围起来
//尝试执行try里的代码 一旦异常发生 发生异常的代码后面的代码不会执行 立即跳到catch块里面执行
//如果try处理了异常,那么发生异常的会执行catch里的代码 执行完之后继续往下执行 程序不会推出
//try其实是比较浪费性能的 只有在有可能发生异常的地方才写
//文件、数据库访问等
//一般情况下,如果抛出的异常是系统异常 那么谁调用谁就去捕捉异常
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 异常处理 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //尝试执行try里的代码 一旦异常发生 发生异常的代码后面的代码不会执行 立即跳到catch块里面执行 14 //如果try处理了异常,那么发生异常的会执行catch里的代码 执行完之后继续往下执行 程序不会推出 15 //try其实是比较浪费性能的 只有在有可能发生异常的地方才写 16 //文件、数据库访问等 17 try 18 { 19 Console.WriteLine("请输入数字"); 20 int.Parse(Console.ReadLine()); 21 Console.WriteLine("是否执行?"); 22 } 23 catch (Exception ex)//如果try里的代码发生异常会将异常信息封装为一个Exception对象 并赋给ex 24 { 25 Console.WriteLine(ex); 26 Console.WriteLine("********************************"); 27 //ex.Message属性封装的是异常的提示信息 28 Console.WriteLine(ex.Message); 29 Console.WriteLine("我被Catch"); 30 } 31 Console.WriteLine("执行完毕"); 32 //尝试执行try里的代码 一旦异常发生 发生异常的代码后面的代码不会执行 立即跳到catch块里面执行 33 //如果try处理了异常,那么发生异常的会执行catch里的代码 执行完之后继续往下执行 程序不会推出 34 35 try 36 { 37 Console.WriteLine("请输入A数字"); 38 int A=int.Parse(Console.ReadLine()); 39 Console.WriteLine("请输入B数字"); 40 int B = int.Parse(Console.ReadLine()); 41 Console.WriteLine(A/B); 42 } 43 catch (Exception ex)//如果try里的代码发生异常会将异常信息封装为一个Exception对象 并赋给ex 44 { 45 if (ex is FormatException) 46 { 47 Console.WriteLine("输入的格式不对"); 48 } 49 else if (ex is DivideByZeroException) 50 { 51 Console.WriteLine("除数不能为0"); 52 } 53 Console.WriteLine("我被Catch"); 54 } 55 56 57 //catch 可有多个不同的异常处理,但是子类在前 父类在后 58 try 59 { 60 Console.WriteLine("请输入A数字"); 61 int A = int.Parse(Console.ReadLine()); 62 Console.WriteLine("请输入B数字"); 63 int B = int.Parse(Console.ReadLine()); 64 Console.WriteLine(A / B); 65 } 66 catch (FormatException ex) 67 { 68 Console.WriteLine(ex.Message); 69 } 70 catch (DivideByZeroException ex) 71 { 72 Console.WriteLine(ex.Message); 73 } 74 catch(Exception) 75 { 76 Console.WriteLine("异常来啦"); 77 } 78 //fianlly 79 try 80 { 81 Console.WriteLine("请输入A数字"); 82 int A = int.Parse(Console.ReadLine()); 83 Console.WriteLine("请输入B数字"); 84 int B = int.Parse(Console.ReadLine()); 85 Console.WriteLine(A / B); 86 } 87 catch (Exception) 88 { 89 Console.WriteLine("异常来啦"); 90 } 91 finally 92 { 93 Console.WriteLine("finally"); 94 } 95 96 try 97 { 98 Console.WriteLine("请输入A数字"); 99 int A = int.Parse(Console.ReadLine()); 100 Console.WriteLine("请输入B数字"); 101 int B = int.Parse(Console.ReadLine()); 102 Console.WriteLine(A / B); 103 } 104 finally 105 { 106 Console.WriteLine("finally"); 107 } 108 Console.WriteLine("执行完毕"); 109 Console.ReadKey(); 110 111 } 112 } 113 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 自定义异常 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 try 14 { 15 Console.WriteLine("请输入A数字"); 16 int A = int.Parse(Console.ReadLine()); 17 Console.WriteLine("请输入B数字"); 18 int B = int.Parse(Console.ReadLine()); 19 if (B >= 100) 20 { 21 throw new MyException("MyException除数不能超过100"); 22 } 23 Console.WriteLine(A / B); 24 } 25 catch (Exception ex) 26 { 27 28 Console.WriteLine(ex.Message); 29 } 30 31 try 32 { 33 Console.WriteLine("请输入A数字"); 34 int A = int.Parse(Console.ReadLine()); 35 Console.WriteLine("请输入B数字"); 36 int B = int.Parse(Console.ReadLine()); 37 if (B >= 100) 38 { 39 throw new Exception("Exception除数不能超过100"); 40 } 41 Console.WriteLine(A / B); 42 } 43 catch (Exception ex) 44 { 45 46 Console.WriteLine(ex.Message); 47 } 48 Console.ReadKey(); 49 } 50 } 51 class MyException : Exception 52 { 53 public MyException(string msg):base(msg) 54 { 55 } 56 } 57 }