try...catch:捕获异常
try...finally:清除异常
try..catch...finily:处理所有异常
1、捕获异常
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 //try...catch捕获异常
10 //try...finally清除异常
11 //try..catch...finily处理所有异常
12 //try
13 //{
14 //}
15 //catch(){}
16
17 class Program
18 {//捕获异常
19 static void Main(string[] args)
20 {
21 //利用try catch语句来捕获数组越界问题
22 int[] myint = {1,2,3,4,5,6,7 };
23 try
24 {
25 for (int i = 0; i <= myint.Length; i++)
26 {
27 Console.Write(myint[i].ToString() + " ");
28 }
29 }
30 //catch
31 //{
32 // Console.WriteLine("异常已经发生");
33 //}
34 //这条语句虽然可以捕获,但不能描述原因
35 catch (Exception e) {
36 Console.WriteLine(e.Message.ToString());
37 }
38 Console.ReadKey();
39 }
40 }
41 }//e.Message.ToString()中的Message是获取描述当前异常信息
2、清除处理所有异常
如果用户对产生的错误不进行处理,只是捕获,但不能消除产生的错误分配的资源,需要用到finally
1 try
2 {
3 //包含容易产生异常的代码
4 }
5 finally
6 {
7 //用于消除try块中分配的任何资源以及运行任何即使在发生异常时也必须执行的代码
8 }
但是没有对异常进行提示,所以选择处理所有异常
1 // try
2 //{//包含容易产生异常的代码
3 //}
4 // catch (异常类,异常实例对象)
5 // {
6 //异常处理代码
7 //}
8 // finally {
9 //用于消除try块中分配的任何资源以及运行任何即使在发生异常时也必须执行的代码,无论发生异常与否
10 //}
示例:除数不能为0的异常捕获与处理
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 int[] myint = { 0, 2, 4, 5, 6, 6 };
14 try
15 {
16 for (int i = 0; i < myint.Length; i++)
17 {
18 Console.WriteLine("720除{0}={1}",myint[i],720/myint[i]);
19 }
20 }
21 catch (Exception e)
22 {
23 Console.WriteLine(e.Message.ToString());
24 }
25 finally
26 {
27 Console.WriteLine("什么时候都会执行,无论发生与否!");
28 }
29 Console.ReadKey();
30 }
31 }
32 }

所有代码中都加上异常处理,完全可以,但处理异常会大大降低性能。如可能检测到发生异常,那么可以添加上,如果没有可能就无需添加。
3、引发异常
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 /// throw引发异常
12 ///
13 /// throw new 异常类(异常信息);
14 /// 异常类:预定义、自定义
15 /// 异常信息:字符串“格式转换错误”
16 /// </summary>
17 /// 实例:
18 /// 为Pro
19 private static int ConvertStringToInt(string mystr)
20 {
21 int outnum = 0;
22 try
23 {
24 outnum = Convert.ToInt32(mystr);
25 return outnum;
26 }
27 catch
28 {
29 throw new FormatException("格式转换是不正确");
30 }
31 }
32 static void Main(string[] args)
33 {
34 string mystr = "haha666";
35 try
36 {
37 int myint;
38 myint = ConvertStringToInt(mystr);
39 Console.WriteLine(myint);
40 }
41 catch (Exception e)
42 {
43 Console.WriteLine(e.Message.ToString());
44 }
45 Console.ReadKey();
46 }
47 }
48 }

4、预定义异常
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 int[] a = { 1,2,3,4,5,6};
14 try
15 {
16 for (int i = 0; i <= a.Length; i++)
17 Console.WriteLine(a[i]);
18 }
19 catch(IndexOutOfRangeException e)//自定义了数组越界的异常
20 {
21 Console.WriteLine(e.Message.ToString());
22 }
23
24 Console.ReadKey();
25 }
26 }
27 }

5、自定义异常类
System.Exception为系统预定义的异常
throw(自定义异常类名);
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 //System.Exception为系统预定义的异常
10 //throw(自定义异常类名);
11 class MyException:Exception
12 {
13
14 }
15 class Program
16 {
17 static void Main(string[] args)
18 {
19 try
20 {
21 Console.WriteLine("这行代码在引发异常之前会被执行");
22 throw new MyException();
23 Console.WriteLine("由于引发了异常,这行代码不会被执行");//系统知道这句不会执行,所以编译前会加上波浪线提示
24 }
25 catch (MyException)
26 {
27 Console.WriteLine("这里是自定义异常");
28
29 }
30 Console.ReadKey();
31 }
32
33 }
34 }