Handling errors from DataWindow
property expressions in PowerBuilder
What causes errors
In PowerBuilder, an invalid DataWindow property expression causes
a runtime error in your application. A runtime error causes the
application to terminate unless you catch the error in a runtime error
handler or unless there is a script for the Error event.
|
Conditions that cause errors |
Possible causes |
|---|---|
|
Invalid names of controls within the DataWindow |
Mistyping, which the compiler does not catch A |
|
A property is not valid for the specified |
Mistyping. The control is a different |
You can prevent the application from terminating by handling the
error in the DataWindow control’s Error event or by catching the error
in a try-catch block.
Responding to errors in the Error event
script
The Error event’s arguments give you several options for
responding to the error. You choose a course of action and set the
action argument to a value of the ExceptionAction enumerated
datatype.
ExceptionAction enumerated datatype
If you give the action argument a value other than
ExceptionIgnore!, you will prevent error-handling code in try-catch
blocks from executing. For more information on values for the
ExceptionAction enumerated datatype, see the Error event description
in the the section called “Error” in PowerScript Reference.
If you are trying to find out a property value and you know the
expression might cause an error, you can include code that prepares for
the error by storing a default value in an instance variable. Then the
Error event script can return that value in place of the failed
expression.
There are three elements to this technique: the declaration of an
instance variable, the script that sets the variable’s default value and
then accesses a DataWindow property, and the Error event script. These
elements are shown in Example 2 below.
Responding to errors in a try-catch
block
You can prevent the application from terminating by handling the
DataWindow runtime error (DWRuntimeError) in a try-catch block. If you
are trying to find out a property value and you know the expression
might cause an error, you can include code that automatically assigns a
valid default value that can be substituted for the failed expression,
as in Example 2 below.
Examples
Example 1
This code displays complete information about the error in a
multilineedit mle_1.
The error event script:
|
1 2 3 4 5 6 7 |
mle_1.text = & "error#: " + string(errornumber) + "~r~n" + & "text: " + errortext + "~r~n" + & "parent: " + errorwindowmenu + "~r~n" + & "object: " + errorobject + "~r~n" + & "line: " + string(errorline) + "~r~n" action = ExceptionIgnore! |
The try-catch block:
|
1 2 3 4 5 6 7 8 9 |
Try ... //DataWindow property expression Catch (DWRuntimeError myExc) mle_1.text = & "error#: " + string(myExc.number) + "~r~n" +& "text: " + myExc.text + "~r~n" + & "script: " + myExc.routinename + "~r~n" + & "object: " + myExc.objectname + "~r~n" + & "line: " + string(myExc.line) + "~r~n" End Try |
If the correct evaluation of the expression is not critical to the
application, the application continues without terminating.
Example 2
This example provides a return value that will become the
expression’s value if evaluation of the expression causes an
error.
There are three elements to code in the error event script. The
instance variable is a string:
|
1 |
string is_dwvalue |
This script for a button or other control stores a valid return
value in an instance variable and then accesses a DataWindow
property:
|
1 2 |
is_dwvalue = "5" ls_border = dw_1.Object.id.Border |
The Error event script uses the instance variable to provide a
valid return value:
|
1 2 |
action = ExceptionSubstituteReturnValue! returnvalue = is_dwvalue |
The try-catch block:
|
1 2 3 4 |
try ls_border = dw_1.Object.id.Border catch (DWRuntimeError myDWError) ls_border = "5" end try |
At runtime, if the id column does not exist or some other error
occurs, then the expression returns a valid border value — here the
string “5”. If you are using the Error event instead of a try-catch
block, you must first store the value in an instance variable.