Create
a new user function and user event
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
Now you add a function that you invoke from the Percentage command
button’s Clicked event and an event that is triggered when the focus is
changed from the drop-down list box on the w_cust_pct window. The function
calculates the percentage of customers living in a particular state. The
event processes the current value of the drop-down list box control to
make sure it is two characters in length (for the state code).
-
Open w_cust_pct in the Window painter if it is not already
open.Select (Functions) in the first list box in the Script
view.The Script view displays the Prototype window. The first
drop-down list box in the Script view displays (Functions) and the
second drop-down list box displays (New Function). -
Select decimal for the Return Type and type uf_percentage for
Function Name. -
Select integer for the Argument Type and type ai_custbystate for
the Argument Name.You will add a second argument in the next step.
-
Right-click anywhere in the Prototype window and select Add
Parameter from the pop-up menu. -
Select integer for the second Argument Type and type
ai_totalcust for the second Argument Name. -
Type exc_no_rows,exc_low_number in the Throws box.
-
Enter the following script for the new function:
123456789101112131415161718192021222324Decimal my_resultexc_no_rows le_nrexc_low_number le_ex/* Process two integers passed as parameters. Instantiate and throw exceptionsif the first integer value is 0 or 1. Otherwise calculate a percentage andreturn a numeric value truncated to a single decimal place. If the secondinteger value is 0, catch and rethrow the runtime dividebyzero error duringthe calculation.*/CHOOSE CASE ai_custbystateCASE 0le_nr = create exc_no_rowsthrow le_nrCASE 1le_ex = create exc_low_numberthrow le_exCASE ELSETRYmy_result=(ai_custbystate/ai_totalcust)*100CATCH (dividebyzeroerror le_zero)throw le_zeroEnd TRYEND CHOOSEreturn truncate(my_result,1)Later in this tutorial, you will call the uf_percentage function
from the Clicked event on the command button, passing in two integers
and processing the return value.You now add a user event for the drop-down list box that throws
the exc_bad_entry exception object when a user-entered state code is
not exactly two characters in length. -
Select ddlb_state in the first drop-down list box of the Script
view and select (New Event) in the second drop-down list box. -
Select integer for Return Type and type ue_modified for Event
Name.Select string for Argument Type and type as_statecode for
Argument Name.Type exc_bad_entry in the Throws box or drag it from the System
Tree to the Throws box.Note that the Event ID is (None). You do not select an Event ID
for the ue_modified event. If you selected an Event ID, you could not
enter user-defined exception objects in the event Throws
clause. -
Enter the following script for the new ue_modified event:
12345678910exc_bad_entry le_ex//Make sure the current text in the drop-down list//box is two characters in length. Otherwise,//instantiate the exc_bad_entry exception object and//throw the exception.IF len(this.text)<>2 Thenle_ex = create exc_bad_entrythrow le_exEND IFReturn 1Next you call the ue_modified event and the uf_percentage
function, and catch the exceptions thrown by these methods.