home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 February / PCWK0297.iso / envelop / envelop.6 / Tools / Bootcamp / concepts / trycatch / trycatch.eto < prev    next >
Text File  |  1996-07-08  |  14KB  |  456 lines

  1. Type ExceptionForm From SampleMasterForm
  2.   Dim Label1 As New Label
  3.   Dim BtnTest1 As New Button
  4.   Dim Label2 As New Label
  5.   Dim OptThrowPoint1 As New OptionButton
  6.   Dim OptThrowPoint2 As New OptionButton
  7.   Dim OptThrowPoint3 As New OptionButton
  8.   Dim Label3 As New Label
  9.   Dim Label4 As New Label
  10.   Dim Label5 As New Label
  11.   Dim BtnTest2 As New Button
  12.   Dim LblNotFound As New Label
  13.   Dim LblTooFewArguments As New Label
  14.   Dim LblFileNotFound As New Label
  15.   Dim LblTooManyArguments As New Label
  16.   Dim Label6 As New Label
  17.   Dim LblTestReThrow As New Label
  18.   Dim LblCatchPoint1 As New Label
  19.   Dim LblCatchPoint2 As New Label
  20.   Dim LblCatchPoint3 As New Label
  21.   Dim Label11 As New Label
  22.   Dim BtnTest3 As New Button
  23.   Dim Label8 As New Label
  24.   Dim BtnTest4 As New Button
  25.   Dim Label9 As New Label
  26.   Dim Label10 As New Label
  27.   Dim LblNumberTooSmall As New Label
  28.   Dim LblNumberTooLarge As New Label
  29.   Dim LblNumberOdd As New Label
  30.   Dim LblNumberEven As New Label
  31.   Dim ChkDebugTrap As New CheckBox
  32.  
  33.   ' METHODS for object: ExceptionForm
  34.   Sub BtnTest1_Click()
  35.     ' Reset the standard colors
  36.     OptThrowPoint1.ForeColor = -1
  37.     OptThrowPoint2.ForeColor = -1
  38.     OptThrowPoint3.ForeColor = -1
  39.     ' Execute the Example
  40.     TestThrow
  41.   End Sub
  42.  
  43.   Sub BtnTest2_Click()
  44.     ' Reset the standard colors
  45.     LblTooFewArguments.ForeColor = -1
  46.     LblTooManyArguments.ForeColor = -1
  47.     LblNotFound.ForeColor = -1
  48.     LblFileNotFound.ForeColor = -1
  49.     ' Test routine for generating system exceptions
  50.     TestSystemException
  51.   End Sub
  52.  
  53.   Sub BtnTest3_Click()
  54.     ' Reset the standard colors
  55.     LblTestReThrow.ForeColor = -1
  56.     LblCatchPoint1.ForeColor = -1
  57.     LblCatchPoint2.ForeColor = -1
  58.     LblCatchPoint3.ForeColor = -1
  59.     ' Execute the example
  60.     TestReThrow
  61.   End Sub
  62.  
  63.   Sub BtnTest4_Click()
  64.     ' Reset the standard colors
  65.     LblNumberTooSmall.ForeColor = -1
  66.     LblNumberTooLarge.ForeColor = -1
  67.     LblNumberOdd.ForeColor = -1
  68.     LblNumberEven.ForeColor = -1
  69.     ' Execute the TestUserException Sub
  70.     TestUserException
  71.   End Sub
  72.  
  73.   Sub CatchPoint1
  74.     Try
  75.       CatchPoint2
  76.     Catch TooFewArguments
  77.       LblCatchPoint1.ForeColor = 255
  78.       InfoBox.Message("", "CatchPoint1: There were too few arguments.")
  79.     End Try
  80.   End Sub
  81.  
  82.   Sub CatchPoint2
  83.     Try
  84.       CatchPoint3
  85.     Catch TooManyArguments
  86.       LblCatchPoint2.ForeColor = 255
  87.       InfoBox.Message("", "CatchPoint2: There were too many arguments.")
  88.     End Try
  89.   End Sub
  90.  
  91.   Sub CatchPoint3
  92.     Try
  93.       GenerateSystemException
  94.     Catch NotFound(error_description as String)
  95.       LblCatchPoint3.ForeColor = 255
  96.       InfoBox.Message("", "CatchPoint3: The test function cannot find '" & error_description & "'")
  97.     End Try
  98.   End Sub
  99.  
  100.   Sub ChkDebugTrap_Click()
  101.     Dim on As Integer
  102.   
  103.     on = (ChkDebugTrap.Value = "Checked")
  104.     Debugger.TrapInterpretiveExceptions = on
  105.     Debugger.TrapSystemExceptions = on
  106.   
  107.   End Sub
  108.  
  109.   Sub GenerateSystemException
  110.     Dim random_number as Integer
  111.     random_number = int(4 * rnd() + 1)
  112.     Select Case random_number
  113.       Case 1
  114.         ' Too few arguments are sent to this function
  115.         AngleArc(12.5)
  116.       Case 2
  117.         ' Too many arguments are sent to this function
  118.         AngleArc(12.5, 11.6, 2.0, 0.0, 45.0, 60.0)
  119.       Case 3
  120.         ' This function invokes an identifier, radius, which
  121.         ' cannot be found
  122.         AngleArc(12.5, 11.6, radius, 0.0, 45.0)
  123.       Case 4
  124.         ' try and open a file that does not exist
  125.         TextFile.FileName = "envelop.test"
  126.         TextFile.Open(True)
  127.     End Select
  128.   
  129.   End Sub
  130.  
  131.   Sub GenerateUserException
  132.     Dim random_number as Integer
  133.     random_number = int(4 * rnd() + 1)
  134.     Select Case random_number
  135.       Case 1
  136.         ' This number is too small
  137.         Throw NumberTooSmall(1)
  138.       Case 2
  139.         ' This number is even
  140.         ' No arguements are being passed back
  141.         Throw NumberEven
  142.       Case 3
  143.         ' This number is odd
  144.         ' We throw the an execption titled "NumberOdd"
  145.         ' with several arguments of different data types
  146.         Throw NumberOdd(3.0, "$5.75")
  147.       Case 4
  148.         ' This number is too large
  149.         Throw NumberTooLarge("4")
  150.     End Select
  151.   
  152.   End Sub
  153.  
  154.   Sub ResetApplication_Click
  155.     ' Reset Example 1.
  156.     ' Reset the standard colors
  157.     OptThrowPoint1.ForeColor = -1
  158.     OptThrowPoint2.ForeColor = -1
  159.     OptThrowPoint3.ForeColor = -1
  160.   
  161.     ' Clear the option buttons
  162.     OptThrowPoint1.Value = False
  163.     OptThrowPoint2.Value = False
  164.     OptThrowPoint3.Value = False
  165.   
  166.     ' Reset Example 2.
  167.     ' Reset the standard colors
  168.     LblTooFewArguments.ForeColor = -1
  169.     LblTooManyArguments.ForeColor = -1
  170.     LblNotFound.ForeColor = -1
  171.     LblFileNotFound.ForeColor = -1
  172.   
  173.     ' Reset Example 3.
  174.     ' Clear the label colors
  175.     LblTestReThrow.ForeColor = -1
  176.     LblCatchPoint1.ForeColor = -1
  177.     LblCatchPoint2.ForeColor = -1
  178.     LblCatchPoint3.ForeColor = -1
  179.   
  180.     ' Reset Example 4.
  181.     ' Reset the standard colors
  182.     LblNumberTooSmall.ForeColor = -1
  183.     LblNumberTooLarge.ForeColor = -1
  184.     LblNumberOdd.ForeColor = -1
  185.     LblNumberEven.ForeColor = -1
  186.   
  187.     ' Set exception trapping off by default
  188.     ChkDebugTrap.Value = "Unchecked"
  189.   End Sub
  190.  
  191.   Sub TestReThrow
  192.     ' This illustrates how various routines can catch certain exceptions
  193.     ' and handle them separately.  Exceptions not caught go up the stack
  194.     ' for further evaluation and handling.
  195.   
  196.     Try
  197.       CatchPoint1
  198.     Catch
  199.       ' Catch any exception that was not previously caught and rethrow
  200.       ' the exception.  The only exception that can reach this
  201.       ' point is the unhandled exception for a file not being found
  202.       LblTestReThrow.ForeColor = 255
  203.       Throw
  204.     End Try
  205.   
  206.   End Sub
  207.  
  208.   Sub TestSystemException
  209.     ' Try a function which will generate a system exception
  210.     ' and in the catch block you would implement code to recover
  211.     ' from the situation.
  212.     Try
  213.       GenerateSystemException
  214.     Catch TooFewArguments
  215.       LblTooFewArguments.ForeColor = 255
  216.       InfoBox.Message("", "Too few arguments were submitted.")
  217.     Catch TooManyArguments
  218.       LblTooManyArguments.ForeColor = 255
  219.       InfoBox.Message("", "Too many arguments were submitted.")
  220.     Catch NotFound(error_description as String)
  221.       LblNotFound.ForeColor = 255
  222.       InfoBox.Message("", "The test function cannot find '" & error_description & "'")
  223.     Catch FileError
  224.       LblFileNotFound.ForeColor = 255
  225.       InfoBox.Message("", "The file was not found.")
  226.     End Try
  227.   End Sub
  228.  
  229.   Sub TestThrow
  230.     Try
  231.       ThrowPoint1
  232.     Catch TestThrowError(error_location As String)
  233.       InfoBox.Message("", "I was thrown from '" & error_location & "'")
  234.     End Try
  235.   End Sub
  236.  
  237.   Sub TestUserException
  238.     Try
  239.       GenerateUserException
  240.     Catch NumberTooSmall(i As Integer)
  241.       LblNumberTooSmall.ForeColor = 255
  242.       InfoBox.Message("", "The number '" & i & "' generated is too small.")
  243.     Catch NumberTooLarge(s As String)
  244.       LblNumberTooLarge.ForeColor = 255
  245.       InfoBox.Message("", "The number '" & s & "' generated is too large.")
  246.     Catch NumberOdd(f as Single, c as Currency)
  247.       LblNumberOdd.ForeColor = 255
  248.       InfoBox.Message("", "The number '" & f & "' generated is worth " & c)
  249.     Catch NumberEven
  250.       LblNumberEven.ForeColor = 255
  251.       InfoBox.Message("", "I think the number generated was even.")
  252.     End Try
  253.   End Sub
  254.  
  255.   Sub ThrowPoint1
  256.     If OptThrowPoint1.Value Then 
  257.       OptThrowPoint1.ForeColor = 255
  258.       Throw TestThrowError("ThrowPoint1")
  259.     Else 
  260.       OptThrowPoint1.ForeColor = 32768
  261.       ThrowPoint2
  262.     End If
  263.   End Sub
  264.  
  265.   Sub ThrowPoint2
  266.     If OptThrowPoint2.Value = True Then 
  267.       OptThrowPoint2.ForeColor = 255
  268.       Throw TestThrowError("ThrowPoint2")
  269.     Else 
  270.       OptThrowPoint2.ForeColor = 32768
  271.       ThrowPoint3
  272.     End If
  273.   End Sub
  274.  
  275.   Sub ThrowPoint3
  276.     If OptThrowPoint3.Value = True Then 
  277.       OptThrowPoint3.ForeColor = 255
  278.       Throw TestThrowError("ThrowPoint3")
  279.     Else 
  280.       OptThrowPoint3.ForeColor = 32768
  281.     End If
  282.   End Sub
  283.  
  284. End Type
  285.  
  286. Begin Code
  287. ' Reconstruction commands for object: ExceptionForm
  288. '
  289.   With ExceptionForm
  290.     .Caption := "Exception Handling Examples"
  291.     .Move(3855, 1770, 9405, 6900)
  292.     .SampleDir := "C:\ENVELOP\bootcamp\concepts\trycatch\"
  293.     .SampleName := "trycatch"
  294.     With .Label1
  295.       .Caption := "1. Nested Throw Example"
  296.       .ForeColor := 16711680
  297.       .ZOrder := 2
  298.       .Move(150, 750, 2850, 300)
  299.     End With  'ExceptionForm.Label1
  300.     With .BtnTest1
  301.       .Caption := "Test 1"
  302.       .ZOrder := 3
  303.       .Move(3750, 750, 900, 300)
  304.     End With  'ExceptionForm.BtnTest1
  305.     With .Label2
  306.       .Caption := "Sub TestThrow"
  307.       .ZOrder := 4
  308.       .Move(450, 1200, 1800, 300)
  309.     End With  'ExceptionForm.Label2
  310.     With .OptThrowPoint1
  311.       .Caption := "Sub ThrowPoint1"
  312.       .ZOrder := 5
  313.       .Move(750, 1650, 2100, 300)
  314.     End With  'ExceptionForm.OptThrowPoint1
  315.     With .OptThrowPoint2
  316.       .Caption := "Sub ThrowPoint2"
  317.       .ZOrder := 6
  318.       .Move(1050, 2100, 2100, 300)
  319.     End With  'ExceptionForm.OptThrowPoint2
  320.     With .OptThrowPoint3
  321.       .Caption := "Sub ThrowPoint3"
  322.       .ZOrder := 7
  323.       .Move(1350, 2550, 1950, 300)
  324.     End With  'ExceptionForm.OptThrowPoint3
  325.     With .Label3
  326.       .Caption := "2. Multiple Catch Example"
  327.       .ForeColor := 16711680
  328.       .ZOrder := 8
  329.       .Move(5100, 750, 3000, 300)
  330.     End With  'ExceptionForm.Label3
  331.     With .Label4
  332.       .Caption := "Sub GenerateSystemException"
  333.       .ZOrder := 9
  334.       .Move(5700, 1650, 3150, 300)
  335.     End With  'ExceptionForm.Label4
  336.     With .Label5
  337.       .Caption := "Sub TestSystemException"
  338.       .ZOrder := 10
  339.       .Move(5400, 1200, 2700, 300)
  340.     End With  'ExceptionForm.Label5
  341.     With .BtnTest2
  342.       .Caption := "Test 2"
  343.       .ZOrder := 11
  344.       .Move(8250, 750, 900, 300)
  345.     End With  'ExceptionForm.BtnTest2
  346.     With .LblNotFound
  347.       .Caption := "NotFound"
  348.       .ZOrder := 12
  349.       .Move(6450, 2625, 1050, 300)
  350.     End With  'ExceptionForm.LblNotFound
  351.     With .LblTooFewArguments
  352.       .Caption := "TooFewArguments"
  353.       .ForeColor := 255
  354.       .ZOrder := 13
  355.       .Move(6450, 2025, 1950, 300)
  356.     End With  'ExceptionForm.LblTooFewArguments
  357.     With .LblFileNotFound
  358.       .Caption := "FileNotFound"
  359.       .ZOrder := 14
  360.       .Move(6450, 2925, 1350, 300)
  361.     End With  'ExceptionForm.LblFileNotFound
  362.     With .LblTooManyArguments
  363.       .Caption := "TooManyArguments"
  364.       .ZOrder := 15
  365.       .Move(6450, 2325, 1950, 300)
  366.     End With  'ExceptionForm.LblTooManyArguments
  367.     With .Label6
  368.       .Caption := "3. Nested Catch/ReThrow Example"
  369.       .ForeColor := 16711680
  370.       .ZOrder := 16
  371.       .Move(150, 3450, 3450, 300)
  372.     End With  'ExceptionForm.Label6
  373.     With .LblTestReThrow
  374.       .Caption := "Sub TestReThrow"
  375.       .ZOrder := 17
  376.       .Move(450, 3900, 1950, 300)
  377.     End With  'ExceptionForm.LblTestReThrow
  378.     With .LblCatchPoint1
  379.       .Caption := "Sub CatchPoint1"
  380.       .ZOrder := 18
  381.       .Move(900, 4350, 1650, 300)
  382.     End With  'ExceptionForm.LblCatchPoint1
  383.     With .LblCatchPoint2
  384.       .Caption := "Sub CatchPoint2"
  385.       .ZOrder := 19
  386.       .Move(1425, 4800, 1650, 300)
  387.     End With  'ExceptionForm.LblCatchPoint2
  388.     With .LblCatchPoint3
  389.       .Caption := "Sub CatchPoint3"
  390.       .ForeColor := 255
  391.       .ZOrder := 20
  392.       .Move(1800, 5250, 1650, 300)
  393.     End With  'ExceptionForm.LblCatchPoint3
  394.     With .Label11
  395.       .Caption := "Sub GenerateSystemException"
  396.       .ZOrder := 21
  397.       .Move(2250, 5700, 3075, 300)
  398.     End With  'ExceptionForm.Label11
  399.     With .BtnTest3
  400.       .Caption := "Test 3"
  401.       .ZOrder := 22
  402.       .Move(3750, 3450, 900, 300)
  403.     End With  'ExceptionForm.BtnTest3
  404.     With .Label8
  405.       .Caption := "4. User Exception Example"
  406.       .ForeColor := 16711680
  407.       .ZOrder := 23
  408.       .Move(5100, 3450, 2700, 300)
  409.     End With  'ExceptionForm.Label8
  410.     With .BtnTest4
  411.       .Caption := "Test 4"
  412.       .ZOrder := 24
  413.       .Move(8250, 3450, 900, 300)
  414.     End With  'ExceptionForm.BtnTest4
  415.     With .Label9
  416.       .Caption := "Sub TestUserException"
  417.       .ZOrder := 25
  418.       .Move(5550, 3900, 2400, 300)
  419.     End With  'ExceptionForm.Label9
  420.     With .Label10
  421.       .Caption := "Sub GenerateUserException"
  422.       .ZOrder := 26
  423.       .Move(5700, 4350, 2850, 300)
  424.     End With  'ExceptionForm.Label10
  425.     With .LblNumberTooSmall
  426.       .Caption := "NumberTooSmall"
  427.       .ZOrder := 27
  428.       .Move(6450, 4800, 2100, 300)
  429.     End With  'ExceptionForm.LblNumberTooSmall
  430.     With .LblNumberTooLarge
  431.       .Caption := "NumberTooLarge"
  432.       .ZOrder := 28
  433.       .Move(6450, 5100, 2100, 300)
  434.     End With  'ExceptionForm.LblNumberTooLarge
  435.     With .LblNumberOdd
  436.       .Caption := "NumberOdd"
  437.       .ZOrder := 29
  438.       .Move(6450, 5400, 2100, 300)
  439.     End With  'ExceptionForm.LblNumberOdd
  440.     With .LblNumberEven
  441.       .Caption := "NumberEven"
  442.       .ZOrder := 30
  443.       .Move(6450, 5700, 2100, 300)
  444.     End With  'ExceptionForm.LblNumberEven
  445.     With .ChkDebugTrap
  446.       .Caption := " Toggle this 'on' to see the Debugger stop when an exception is thrown."
  447.       .ForeColor := 16711680
  448.       .ZOrder := 1
  449.       .Move(150, 150, 9000, 300)
  450.     End With  'ExceptionForm.ChkDebugTrap
  451.     With .helpfile
  452.       .FileName := "C:\ENVELOP\bootcamp\concepts\trycatch\trycatch.hlp"
  453.     End With  'ExceptionForm.helpfile
  454.   End With  'ExceptionForm
  455. End Code
  456.