Run the application

Where you are

   Add a new sheet window to the existing application

   Create user-defined exception objects

   Create a new user function and user event

   Call the methods and catch the exceptions

>   Run the application

You are now ready to run the application and calculate the percentage of customers in a selected state.

You can test the exception conditions you scripted, but to test the divide-by-zero error condition, you need to artificially set the number of customers in the database to zero. You do this by adding a check box to the sheet window, then setting the number of customers to zero if the check box is selected.

In this exercise you:

Test the new sheet window

  1. Click the Run button () in the PowerBar.

    If PowerBuilder prompts you to save changes, click Yes.

  2. Type dba in the User ID box.

    Type sql in the Password box and click OK.

    The database connection is established, and the MDI frame for the application displays.

  3. Select File>Report>Customer Location from the menu bar.

    The Customer Location window displays. The current entry in the drop-down list is AB for Alberta.

  4. Click the Percentage button.

    Because there is only one customer in Alberta, the exc_low_number user-defined exception is thrown. The message from the exception is displayed in a message box that was defined in a CATCH clause in the button Clicked event.

  5. Click OK to close the message box.

    The text in the static text boxes now displays the number of customers in Alberta and the total number of customers in the database. The text in the editable text box tells you the value was not calculated and prompts you to select another state.

  6. Select or type CA in the drop-down list box and click the Percentage button.

    The results from the database show 10 customers in California for a total of 7.9% of all customers in the database. The percentage may be different if you have modified the database.

  7. Type Ohio into the drop-down list box and click the Percentage button.

    When you lose focus from the drop-down list box by clicking the Percentage button control, the LoseFocus event fires. This event calls the ue_modified event that throws the exc_bad_entry user-defined exception. The exception message tells you to use a two-letter postal code for the state name.

  8. Click OK to close the message box, type US in the drop-down list box, and click the Percentage button.

    Because no rows are found in the database with US as the state code, the exc_no_rows exception is thrown. A message displays indicating no rows have been returned and suggests reasons why that might be the case. A more robust application might compare the typed text to a list of state codes and throw the exc_bad_entry exception instead, letting you know that US is not a state code.

  9. Click OK to close the message box.

  10. Right-click the database icon for the Demo Database, a red and yellow SQL symbol (), in your Windows System Tray.

  11. Select Shut down from the pop-up menu, and click Yes in the Warning message box that displays.

    This shuts down the connection to the Demo Database.

  12. Select or type AB again in the drop-down list box and click the Percentage button.

    The message from the exc_no_rows exception object displays for Alberta because the connection to the database was closed. To obtain results again, you need to terminate the application and restart it. PowerBuilder reestablishes a connection to the database at runtime when you restart the application.

  13. Click OK to close the message box and select File>Exit from the application menu to return to the development environment.

    The Database painter and the Database Profile painter might still list the database connection as being open. In this case you can use either painter to disconnect and reconnect to the database at design time.

Add a test for the divide-by-zero error

You now add a check box to the w_cust_pct window. You then write code to force a divide-by-zero error if the check box is selected. Because this test requires an instantiated check box object, you surround the new code in a TRY-CATCH statement that checks for null object errors.

  1. Make sure the w_cust_pct window is open in the Layout view.

    Select Insert>Control>CheckBox from the Window painter menu.

  2. Click in the window just to the right of the Percentage command button.

  3. In the Name box in the Properties view, type cbx_zero.

    In the Text box, type Test divide-by-zero error.

  4. Click the Function List tab.

    Double-click the uf_percentage function.

  5. Type the following text just above the CHOOSE CASE statement:

    //Set denominator to zero to test error condition
    //Numerator unimportant, avoid user exception cases
    TRY
    IF cbx_zero.checked=TRUE THEN
          ai_totalcust=0
          ai_custbystate=2
    END IF
    CATCH (nullobjecterror e_no)
          MessageBox ("Null object", "Invalid Test")
    END TRY
    

    Testing for the null object error

    After you finish this lesson, you can test for the null object error by adding the following line above the TRY statement: DESTROY cbx_zero.

  6. Click the Run button in the PowerBar.

    If PowerBuilder prompts you to save changes, click Yes.

  7. Type dba in the User ID box.

    Type sql in the Password box and click OK.

    The database connection is established, and the MDI frame for the application displays.

  8. Select File>Report>Customer Location from the menu bar.

    Select a state code from the drop-down list box.

  9. Select the Test divide-by-zero error check box.

  10. Click the Percentage button.

    The division by zero error is thrown during the percentage calculation and caught by the button Clicked event. The message box that you coded in the CATCH clause for this error displays.

  11. Click No to continue running the application.

    Continue to test the application by selecting another state code and optionally clearing the check box.

    If the check box is selected when you click the button again and you select Yes in the error message box, the application closes and you return to the development environment.

  12. Close the application when you have finished testing it.