1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Windows.Forms; 6 7 namespace 异常处理_try_catch 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //proTest_Try_Catch(222222299, 0, "/"); 14 proTest_Try_Catch(100, 10, "+"); 15 Console.ReadLine(); 16 } 17 /// <proTest_Try_Catch> 18 /// 19 /// 摘要: 20 /// 测试异常处理 : try..catch 21 /// 22 /// </proTest_Try_Catch> 23 /// <param name="obj1", 操作数1 ></param> 24 /// <param name="obj2", 操作数2 ></param> 25 /// <param name="Operators", 运算符 ></param> 26 static void proTest_Try_Catch(int obj1, int obj2, string strOperators) 27 { 28 var result = 0; 29 try 30 { 31 switch ( strOperators ) 32 { 33 case "+": 34 result = obj1 + obj2; 35 Console.WriteLine(result); 36 break; 37 case "-": 38 result = obj1 - obj2; 39 Console.WriteLine(result); 40 break; 41 case "*": 42 result = obj1 * obj2; 43 Console.WriteLine(result); 44 break; 45 case "/": 46 result = obj1 / obj2; 47 Console.WriteLine(result); 48 break; 49 } 50 if ( result > 100) 51 { 52 throw new ApplicationException("哥的自定义异常消息!!!!!!!!!!!!!!!"); 53 } 54 } 55 catch (Exception e) // 捕获【"哥的自定义异常消息!!!!!!!!!!!!!!!"】 56 { 57 MessageBox.Show("二货 : " + e.Message); 58 return; // 就算有 return 异常处理还是会走到finally; 59 } 60 finally // 必须执行的代码 61 { 62 MessageBox.Show("哈哈哈"); 63 } 64 65 } 66 } 67 }