异常处理和java的语法差不多,先来说一下里面用到的异常类,这些异常类在我们java编程的时候也经常可见:

OverflowException:算数运算、类型转换或转换操作导致溢出所放生的异常;

try{ checked //使用checked关键字 { int i1; int i2; int num; i1 = 5000000; i2 = 5000000; num = i1 * i2; }}catch (OverflowException){ Console.WriteLine("引发OverflowException异常");}

********checked关键字一直在查,查到溢出的时候捕捉异常;

DivideByZeroException:除数为0的发生异常;

FormatException:输入的格式不符合规格发生的异常;

ArithmeticException:算数运算期间发生的异常;

IndexOutOfRangeException:使用小于0或超出数组界限的坐标时发生的异常;

NullReferenceExcption:需要引用对象时引用为null时发生的异常;

最下面的两个异常经常在写java程序的时候都见到。。。

我看的书上有个自定义异常输出的实例,感觉对异常处理写得比较简单:

class Sec{ public int myint(string a, string b) { int int1, int2, num; try { int1 = int.Parse(a); int2 = int.Parse(b); if (int2 == 0) { throw new DivideByZeroException();//如果分母为0抛出异常 } num = int1 / int2; return num; } catch (DivideByZeroException de)//捕获异常 { Console.WriteLine("用零除整数引发异常!"); Console.WriteLine(de.Message); return 0; } } static void Main1(string[] args) { try { Console.WriteLine("请输入分子:"); string str1 = Console.ReadLine(); Console.WriteLine("请输入分母:"); string str2 = Console.ReadLine(); Sec sec = new Sec(); Console.WriteLine("分子除以分母:" + sec.myint(str1, str2)); } catch (FormatException) { Console.WriteLine("请输入数值格式数据"); } }}