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:
Click the Run button in the PowerBar.
If PowerBuilder prompts you to save changes, click Yes.
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.
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.
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.
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.
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.
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.
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.
Click OK to close the message box.
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.
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.
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.
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.
Make sure the w_cust_pct window is open in the Layout view.
Select Insert>Control>CheckBox from the Window painter menu.
Click in the window just to the right of the Percentage command button.
In the Name box in the Properties view, type cbx_zero
.
In the Text box, type Test divide-by-zero
error
.
Click the Function List tab.
Double-click the uf_percentage function.
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
Click the Run button in the PowerBar.
If PowerBuilder prompts you to save changes, click Yes.
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.
Select File>Report>Customer Location from the menu bar.
Select a state code from the drop-down list box.
Select the Test divide-by-zero check box.
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.
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.
Close the application when you have finished testing it.