Implement the finally block for Exception handling

you have the try and catch blocks but no finally... the finally block can be very useful because it gets executed regardless of weather an exception is thrown. This will allow you to do clean up code with out puting it in two places.

Here's an example of how code looks without the finally block:
try
    MoveTo "SomeButton"
    MouseButtonDown 1
    WaitFor 2.0, "ButtonInDownState"
    MouseButtonUp 1 // release the mouse
catch
    MouseButtonUp 1
    // report the error etc...
end try

Here's the same example with the finally block implemented:
try
    MoveTo "SomeButton"
    MouseButtonDown 1
    WaitFor 2.0, "ButtonInDownState
catch
    // report the error etc...
finally
    MouseButtonUp 1 // gets executed regardless of if the testcase fails
end try

Comments

  • bollobollo Member
    what exactly would be the difference from the following code?
    try
    	MoveTo "SomeButton"
    	MouseButtonDown 1
    	WaitFor 2.0, "ButtonInDownState"
    catch
    	// report the error etc...
    end try
    
    MouseButtonUp 1 // release the mouse
    
    
  • EggplantMattEggplantMatt ForumAdmin admin
    I believe that in mtilburg's example, you can rethrow the exception in the catch block (after taking whatever other actions are required) and the code in the finally block will still be executed. In bollo's example, if the catch block were to rethrow the exception, the MouseButtonUp 1 command would not be executed.
Sign In or Register to comment.