Run the application

Note

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.

    Run button

    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.

    Shown is the Customer Location window. At top is the text " 1 Select or type a state in drop down list box " and a drop down set to  A B. Below this is the text "2 Click Percentage button " , then the Percentage button, and then a text box with the entry " Text box for percent of customers in the selected state".
  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.

    Shown is the Customer Location window. At top is the text " Customers in state : 1 " and a drop down set to  A B. Next is the text " total number of customers : 126 " , then the Percentage button, then a text box with the entry "Value not calculated for A B. Try 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.

    Shown is the Customer Location window. At top is the text " Customers in state : 10  " and a drop down set to C A. Next is the text " total number of customers : 126 " ,  then the Percentage button, then a text box with the entry " 7 point 9 percent of customers in C A. "
  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 EAS Demo DB, a red and yellow SQL symbol, in your Windows System Tray.

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

    This shuts down the connection to the EAS Demo DB database.

    Database icon
  11. 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.

  12. 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
    

    NoteTesting 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 check box.

    Shown is the Customer Location window. At top is the text " 1 Select or type a state in drop down list box " and a drop down set to  C O. Below this is the text " 2 Click Percentage button " , then the Percentage button amd a selected check box next to it labeled "Text divide by zero error". At bottom is a text box with the entry " Text box for percent of customers in the selected state".
  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.

    Shown is a message box titled Error. No customers. It displays an X icon and the question Terminate Application?
  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.