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 different DataWindow object has been inserted in the control |
|
A property is not valid for the specified |
Mistyping. The control is a different type than expected. |
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 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 |
mle_1.text = &<br>   "error#: " + string(errornumber) + "~r~n" + &<br>   "text: " + errortext + "~r~n" + &<br>   "parent: " + errorwindowmenu + "~r~n" + &<br>   "object: " + errorobject + "~r~n" + &<br>   "line: " + string(errorline) + "~r~n"<br>action = ExceptionIgnore! |
The try-catch block:
|
1 |
Try<br>   ... //DataWindow property expression<br>Catch (DWRuntimeError myExc)<br>   mle_1.text = &<br>   "error#: " + string(myExc.number) + "~r~n" +&<br>   "text: " + myExc.text + "~r~n" + &<br>   "script: " + myExc.routinename + "~r~n" + &<br>   "object: " + myExc.objectname + "~r~n" + &<br>   "line: " + string(myExc.line) + "~r~n"<br>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 |
is_dwvalue = "5"<br>ls_border = dw_1.Object.id.Border |
The Error event script uses the instance variable to provide
a valid return value:
|
1 |
action = ExceptionSubstituteReturnValue!<br>returnvalue = is_dwvalue |
The try-catch block:
|
1 |
try<br>   ls_border = dw_1.Object.id.Border<br>catch (DWRuntimeError myDWError)<br>   ls_border = "5"<br>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.