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

  1. 
  2. Imports System
  3. Imports Microsoft.VisualBasic
  4.  
  5. Class App
  6.     
  7.     ' This method is called when the Button object is clicked
  8.     Shared Function Divide(numerator As Int32, denominator As Int32) As Int32
  9.         
  10.         Console.WriteLine("Validating the parameters to the Divide method")
  11.         
  12.         ' Validate the method's parameters
  13.         If denominator = 0 Then
  14.             Console.WriteLine("The denominator can't be 0, throwing an exception to indicate error")
  15.             Throw New ArgumentOutOfRangeException("denominator", "denominator can't be 0")
  16.         End If
  17.         
  18.         Console.WriteLine("Divide method is running ok")
  19.         Return CType(numerator / denominator, Int32)
  20.     End Function 'Divide
  21.     
  22.     
  23.     
  24.     Shared Sub Main()
  25.         
  26.         ' A try block indicates code requiring common error recovery or common cleanup code
  27.         Try
  28.             Console.WriteLine("---> About to call Divide the 1st time")
  29.             Console.WriteLine(("6 / 2 = " & Divide(6, 2)))
  30.             
  31.             Console.WriteLine(ControlChars.CrLf & "---> About to call Divide the 2nd time")
  32.             Console.WriteLine(("5 / 0 = " & Divide(5, 0)))
  33.             
  34.             Console.WriteLine("This never executes because an exception is thrown earlier")
  35.         Catch argException As ArgumentOutOfRangeException
  36.           ' The catch block runs if an ArgumentOutOfRange excetpion is thrown
  37.           Console.WriteLine("An error occurred while executing code within the try block")
  38.           Console.WriteLine(argException.Message)
  39.           Console.WriteLine("We are gracefully recoverring and continuing to execute")
  40.         Finally
  41.             ' The finally block is where you put any and all common cleanup tasks
  42.             Console.WriteLine("Executing common cleanup tasks")
  43.         End Try 
  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.         Console.WriteLine(ControlChars.CrLf & "---> The method continues to run...")
  50.  
  51.         Console.Write("Press Enter to close window...")
  52.     Console.Read()
  53.     End Sub 'Main
  54. End Class 'App