200 likes | 466 Views
异常处理. 异常( Exception )是运行时产生的错误。使用 C# 的异常处理子系统,我们能够以标准化可控制的方式来处理运行时错误。 异常处理可以说是对不正常情况的处理,包括发生错误时的处理,异常处理通过允许程序定义代码块来简化错误处理,此代码块称为异常处理程序,出现错误时自动执行它。. System.Exception 类. 在 C# 中,异常用类来表示,所有异常类都必须从内部异常类 Exception 派生而来的,而 Exception 是 System 名字空间的一部分。因此所有异常都是 Exception 的子类。.
E N D
异常处理 异常(Exception)是运行时产生的错误。使用C#的异常处理子系统,我们能够以标准化可控制的方式来处理运行时错误。 异常处理可以说是对不正常情况的处理,包括发生错误时的处理,异常处理通过允许程序定义代码块来简化错误处理,此代码块称为异常处理程序,出现错误时自动执行它。 System.Exception类 在C#中,异常用类来表示,所有异常类都必须从内部异常类 Exception派生而来的,而Exception是 System名字空间的一部分。因此所有异常都是 Exception 的子类。
在C#中有两类常规异常:C#运行时系统(CLR)产生的异常(SystemException),以及应用程序产生的异常(ApplicationException)。在C#中有两类常规异常:C#运行时系统(CLR)产生的异常(SystemException),以及应用程序产生的异常(ApplicationException)。 C#中定义的常用标准异常都是从SystemException派生而来的,例如: System.OutOfMemoryException 当试图通过 new 来分配内存而失败时抛出。 System.IndexOutOfRangeException 当试图通过一个比零小或者超出数组边界的标签来索 引一个数组时抛出。 DivideByZeroException 当试图用整数类型数据除以零时抛出。 System.NullReferenceException 试图对空引用进行操作,也就是说引用没有指向对象时抛出。 OverflowException. 当一个算术操作溢出时抛出。
异常 怎样被处理 C#中异常处理由4个关键字来管理:try、 catch、throw和 finally。 工作方式如下:要监视是否产生异常的程序语句包含在try模块中。如果try模块内产生异常,那么系统自动抛出此异常,同时也可以手动用(throw)抛出异常,然后使用catch捕捉此异常,并以合理的方式处理它。 异常处理语法 try { // suspect code } catch { // handle exceptions }
// Demonstrate exception handling. using System; class ExcDemo1 { public static void Main() { int[] nums = new int[4]; try { Console.WriteLine("Before exception is generated."); // Generate an index out-of-bounds exception. for(int i=0; i < 10; i++) { nums[i] = i; Console.WriteLine("nums[{0}]: {1}", i, nums[i]); } Console.WriteLine("this won't be displayed"); }
catch (IndexOutOfRangeException) { // catch the exception Console.WriteLine("Index out-of-bounds!"); } Console.WriteLine("After catch statement."); } } 输出结果: Before exception is generated. nums[0]: 0 nums[1]: 1 nums[2]: 2 nums[3]: 3 Index out-of-bounds! After catch statement.
请注意,catch子句中不指定参数,只有需要访问异常对象时才需要参数。在某些情况下,异常处理程序能够使用异常对象的值来获得此错误的附加信息。请注意,catch子句中不指定参数,只有需要访问异常对象时才需要参数。在某些情况下,异常处理程序能够使用异常对象的值来获得此错误的附加信息。 Message---获取描述当前异常的消息. Source获取或设置导致错误的应用程序或对象的名称。 ToString()---创建并返回当前异常的字符串表示形式 using System; class ExceptionTest { public static void Main(string[] args) { int a=30; int b=0; int c = 0; try {c=a/b;}
catch (DivideByZeroException e) { Console.WriteLine("e.Message= "+e.Message); Console.WriteLine("e.Source= "+e.Source); Console.WriteLine("e.ToString()= "+e.ToString()); return; } Console.WriteLine("a={0} b={1} a/b={2}", a,b,c); } } 输出结果: e.Message= 试图除以零。 e.Source= ConsoleApplication8 e.ToString()= System.DivideByZeroException: 试图除以零。 at ExceptionTest.Main(String[] args) in c:\documents and settings\whtc\my documents\visual studio projects\consoleapplication8\consoleapplication8\class1.cs: line 11
异常允许合理处理错误 异常处理的关键优点之一是:它允许程序响应错误并继续运行。例如:下列数组的一个元素除以另一个元素。如果出现除数为0的情况,那么产生DivideByZeroException异常。在此程序中,此异常的处理是报告错误消息并继续执行。 using System; class ExcDemo3 { public static void Main() { int[] numer = { 4, 8, 16, 32, 64, 128 }; int[] denom = { 2, 0, 4, 4, 0, 8 }; for(int i=0; i < numer.Length; i++) { try { Console.WriteLine(numer[i] + " / " + denom[i] + " is " + numer[i]/denom[i]); }
catch (DivideByZeroException) { // catch the exception Console.WriteLine("Can't divide by Zero!"); } } } } 输出结果: 4 / 2 is 2 Can't divide by Zero! 16 / 4 is 4 32 / 4 is 8 Can't divide by Zero! 128 / 8 is 16 请注意:异常被处理后就从系统删除。
使用多个catch语句 可以有多个语句与同一个try模块相关联。每一个catch语句必须捕捉不同类型的异常,异常处理的顺序是至关重要的,例如:Sytem.DivideByZeroException派生自System.ArthmeticException。 如果试图在捕获DivideByZeroException之前捕获ArthmeticException,那么程序将永远捕获不到DivideByZeroException。因为DivideByZeroException是ArthmeticException类型,也就是说它派生自ArthmeticException。
// Use multiple catch statements. using System; class ExcDemo4 { public static void Main() { // Here, numer is longer than denom. int[] numer = { 4, 8, 16, 32, 64, 128, 256, 512 }; int[] denom = { 2, 0, 4, 4, 0, 8 }; for(int i=0; i < numer.Length; i++) { try { Console.WriteLine(numer[i] + " / " + denom[i] + " is " + numer[i]/denom[i]); } catch (DivideByZeroException) { // catch the exception Console.WriteLine("Can't divide by Zero!"); }
catch (IndexOutOfRangeException) { // catch the exception Console.WriteLine("No matching element found."); } } } } 输出结果: 4 / 2 is 2 Can't divide by Zero! 16 / 4 is 4 32 / 4 is 8 Can't divide by Zero! 128 / 8 is 16 No matching element found. No matching element found.
捕捉所有异常 有时我们想要捕捉所有异常,而不管其类型,此时使用没有参数的catch语句。 using System; class ExcDemo5 { public static void Main() { int[] numer = { 4, 8, 16, 32, 64, 128, 256, 512 }; int[] denom = { 2, 0, 4, 4, 0, 8 }; for(int i=0; i < numer.Length; i++) { try { Console.WriteLine(numer[i] + " / " + denom[i] + " is"+numer[i]/denom[i]); } catch { Console.WriteLine("Some exception occurred."); } } } }
输出结果如下: 4 / 2 is 2 Some exception occurred. 16 / 4 is 4 32 / 4 is 8 Some exception occurred. 128 / 8 is 16 Some exception occurred. Some exception occurred.
throw抛出异常 前面的示例捕捉C#自动产生的异常。但是通过使用throw语句能够手动抛出(产生)异常。 using System; class ThrowDemo { public static void Main() { try { Console.WriteLine("Before throw."); throw new DivideByZeroException(); } catch (DivideByZeroException) { // catch the exception Console.WriteLine("Exception caught."); } Console.WriteLine("After try/catch block."); } } 输出结果: Before throw. Exception caught. After try/catch block.
finally语句 有时我们想要定义离开try/catch模块时必须执行的代码块,可以用finally语句。 try { // suspect code } catch { // handle exceptions } finally { // always do this } try模块不论是正常结束还是产生异常而结束,最后执行的代码是fnally模块中定义的代码。
示例六: using System; class Finally { public static void Main(string[] args) { int a=30; int b=0; int c = 0; try {c=a/b; } catch (DivideByZeroException e) { Console.WriteLine("除以0所产生的异常: "+e.Message); return; } finally {Console.WriteLine("a={0} b={1} c={2}", a,b,c); } }}
输出结果: 除以0所产生的异常: 试图除以零。 a=30 b=0 c=0 自定义异常类 创建异常的过程很简单,只要定义一个从ApplicationException派生的类即可。 using System; public class MyException : ApplicationException { private static string myMessage=“自定义异常类MyException"; public MyException() : base(myMessage) { } public MyException(string msg) : base(msg) { } }
class MyDivide { public static void Main(string[] args) { int a=40; int b=0; int c = 0; if (b==0) {try {throw new MyException();} catch (MyException e) { Console.WriteLine("除以0所产生的异常:"+e.Message); return; } } else c=a/b; Console.WriteLine("a={0} b={1} a/b={2}", a,b,c); } }
输出结果如下: 除以0所产生的异常:自定义异常类MyException