home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / dotNETSDK / SETUP.EXE / netfxsd1.cab / FL_Exceptions_cs________.3643236F_FC70_11D3_A536_0090278A1BB8 < prev    next >
Encoding:
Text File  |  2001-08-21  |  2.1 KB  |  56 lines

  1. using System;
  2.  
  3.  
  4. class App {
  5.    // This method is called when the Button object is clicked
  6.    static Int32 Divide(Int32 numerator, Int32 denominator) {
  7.  
  8.       Console.WriteLine("Validating the parameters to the Divide method");
  9.  
  10.       // Validate the method's parameters
  11.       if (denominator == 0) {
  12.  
  13.          Console.WriteLine("The denominator can't be 0, throwing an exception to indicate error");
  14.          throw new ArgumentOutOfRangeException("denominator", "denominator can't be 0");
  15.       }
  16.  
  17.       Console.WriteLine("Divide method is running ok");
  18.       return numerator / denominator;
  19.    }
  20.  
  21.  
  22.    static void Main() {
  23.  
  24.       // A try block indicates code requiring common error recovery or common cleanup code
  25.       try {
  26.          Console.WriteLine("---> About to call Divide the 1st time");
  27.          Console.WriteLine("6 / 2 = " + Divide(6, 2));
  28.  
  29.          Console.WriteLine("\n---> About to call Divide the 2nd time");
  30.          Console.WriteLine("5 / 0 = " + Divide(5, 0));
  31.  
  32.          Console.WriteLine("This never executes because an exception is thrown earlier");
  33.       }
  34.       catch (ArgumentOutOfRangeException argException) {
  35.          // The catch block runs if an ArgumentOutOfRange excetpion is thrown
  36.          Console.WriteLine("An error occurred while executing code within the try block");
  37.          Console.WriteLine(argException.Message);
  38.          Console.WriteLine("We are gracefully recoverring and continuing to execute");
  39.       }
  40.       finally {
  41.          // The finally block is where you put any and all common cleanup tasks
  42.          Console.WriteLine("Executing common cleanup tasks");
  43.  
  44.          // This sample doesn't have any common cleanup tasks.
  45.          // However, if you opened or created a file, created a bitmap, established a 
  46.          // network connection, and so on earlier, you should put the corresponding 
  47.          // cleanup code here in the finally block to close the file, dispose the 
  48.          // bitmap, disconnect the connection, etc.
  49.       }
  50.       Console.WriteLine("\n---> The method continues to run...");
  51.  
  52.       Console.Write("Press Enter to close window...");
  53.       Console.Read();
  54.    }
  55. }
  56.