-- 作者:admin
-- 发布时间:11/9/2004 2:25:00 AM
-- C#快餐-8
发信人: nice (春天), 信区: DotNET 标 题: C#快餐-8 发信站: BBS 水木清华站 (Thu Jul 26 02:09:18 2001) Lesson 8. Exceptions There are no exceptions in C and in C++ one can get away from using them with error handling functions such as exit() and terminate(). In C# these functions are absent and we introduce exceptions which take their place. When a program has a bug we can intercept it in the flow of execution by inserting an error handling statement. For example let's say we have a program which does not accesses the first element of an array. Then, during some array handling operations, if an access to array[0] is attempted, the OutOfRange exception is thrown. class OutOfRange is an exception class. All exception classes inherit from class. Compiler will give an error if the inheritance from the Exception class is not made. In the code that follows we throw an exception OutOfRange if i == 0 is true. The try statement is enclosing the part of the code where the exception is expected. If the exception occurs it is handled by the code inside the catch statement. The catch statement takes as an argument an instance of class System.Exception. //first exception handling program using System; class OutOfRange: Exception{ } class Demo { int n; public int []array; public Demo ( int n) { this.array = new int[n]; this.n = n; } public void show_element (int i) { try { if (i == 0) throw ( new OutOfRange()); } catch (Exception e) { Console.WriteLine("Exception : {0}", e); } Console.WriteLine (array [i]); } } class Test { public static void Main() { Demo test = new Demo (3); test.array [1] = 2; test.array [2] = 3; test.show_element (0); } } The output is : Exception : OutOfRange: An exception of type OutOfRange was thrown. at Demo. show_element(Int32 i) in G:\CSharpEd\excep.cs:line 19 0 The previous program can be simplified if you use a built in Exception class provided by the C# runtime. The full list of built in Exception classes hasn't been published yet by Microsoft at the time of this writing. OutOfMemoryException Thrown when an allocation of memory fails. StackOverflowException Infinite recursion. IndexOutOfRangeException Out of bounds access to an array ArithmeticException A base class for exceptions DivideByZeroException and O verflowException OverflowException Overlflow DivideByZeroException Division by zero In the following program I use built in ArgumentException instead of OutOfRange exception. //a better way to handle an Argument exception using System; class Demo { int n; public int []array; public Demo (int n) { this .array = new int [n]; this .n = n; } public void show_element (int i) { try { if (i == 0) throw new ArgumentException ("Out of rannge"); } catch (ArgumentException e) { Console.WriteLine("Exception : {0}", e); return ; } Console.WriteLine (array [i]); } } class Test { public static void Main() { Demo test = new Demo(3); test.array [1] = 2; test.array [2] = 3; test.show_element (0); } } Write a simple program which divides two integers and try using DivideByZero Exception exception. Good Luck! -- ※ 修改:·walts 於 Jul 26 10:10:41 修改本文·[FROM: 166.111.142.118] ※ 来源:·BBS 水木清华站 smth.org·[FROM: 166.111.176.234] 上一篇 返回上一页 回到目录 回到页首 下一篇
|