Manejo de Erorres - Handling DataWindow Errors - PowerBuilder 9

Embed Size (px)

Citation preview

  • 5/22/2018 Manejo de Erorres - Handling DataWindow Errors - PowerBuilder 9

    http:///reader/full/manejo-de-erorres-handling-datawindow-errors-powerbuilde

    PowerBuilder 9.0 Index Page/ DataWindow Programmer's Guide/ Chapter 2 UsingDataWindow Objects/ Handling DataWindow errors

    Handling DataWindow errors

    There are several types of errors that can occur during DataWindow processing:

    Data items that are invalid (discussed in "Manipulating data in a DataWindowcontrol")Failures when retrieving or updating dataAttempts to access invalid or nonexistent properties or data

    This section explains how to handle the last two types of errors.

    Retrieve and Update errors and the DBError event

    Retrieve and update testing

    When using the Retrieve or Update method in a DataWindow control, you should tethe method's return code to see whether the activity succeeded.

    Do not test the SQLCode attribute After issuing a SQL statement (such asCONNECT, COMMIT, or DISCONNECT) or the equivalent method of the transactionobject, you should always test the success/failure code (the SQLCode attribute in ttransaction object). However, you should notuse this type of error checking followiretrieval or update made in a DataWindow.

    For more information about error handling after a SQL statement, see:

    PowerBuilderThe chapter on using transaction objects inApplication TechniqWeb ActiveXChapter 8, "Using the DataWindow Web Control for ActiveX"

    Table 2-4: Return codes for the Retrieve and Update methods

    Method Return code Meaning

    Retrieve >=1 Retrieval succeeded; returns the number of rows retrieve

    -1 Retrieval failed; DBError event triggered.

    0 No data retrieved.

    Update 1 Update succeeded.

    -1 Update failed; DBError event triggered.

    Example

    PowerBuilderIf you want to commit changes to the database only if an updatesucceeds, you can code:

    IF dw_emp.Update() > 0 THEN

    COMMIT USING EmpSQL;

    ELSE

    ROLLBACK USING EmpSQL;

    Helpdoc-online/ PowerBuilder 9.0

    http://en.helpdoc-online.com/powerbuilder_9.0/source/dwprgugp10.htm#x-ref354815179http://en.helpdoc-online.com/powerbuilder_9.0/source/dwprgugp5.htmhttp://en.helpdoc-online.com/powerbuilder_9.0/index.htmlhttp://en.helpdoc-online.com/powerbuilder_9.0/index.htmlhttp://en.helpdoc-online.com/index.htmlhttp://en.helpdoc-online.com/powerbuilder_9.0/source/dwprgugp48.htm#chdcdhdghttp://en.helpdoc-online.com/powerbuilder_9.0/source/dwprgugp10.htm#x-ref354815179http://en.helpdoc-online.com/powerbuilder_9.0/source/dwprgugp12.htmhttp://en.helpdoc-online.com/powerbuilder_9.0/source/dwprgugp5.htmhttp://en.helpdoc-online.com/powerbuilder_9.0/_datawindow_programmers_guide.htmlhttp://en.helpdoc-online.com/powerbuilder_9.0/index.html
  • 5/22/2018 Manejo de Erorres - Handling DataWindow Errors - PowerBuilder 9

    http:///reader/full/manejo-de-erorres-handling-datawindow-errors-powerbuilde

    END IF

    Web ActiveXTo commit changes to the database only if an update succeeds, you ccode:

    number rtn;

    rtn = dw_emp.Update( );

    if (rtn == 1) {

    trans_a.Commit( );

    } else {

    trans_a.Rollback( );

    }

    Using the DBError event

    The DataWindow control triggers its DBError event whenever there is an error folloa retrieval or update; that is, if the Retrieve or Update methods return -1. For examif you try to insert a row that does not have values for all columns that have been

    defined as not allowing NULL, the DBMS rejects the row and the DBError event istriggered.

    By default, the DataWindow control displays a message box describing the errormessage from the DBMS:

    In many cases you may want to code your own processing in the DBError event ansuppress the default message box. Here are some tips for doing this:

    Table 2-5: Tips for processing messages from DBError event

    To Do this

    Get the DBMS's error code

    Use the SQLDBCode argument of the DBError

    event.

    Get the DBMS's message textUse the SQLErrText argument of the DBErrorevent.

    Suppress the default messagebox

    Specify an action/return code of 1.

    About DataWindow action/return codes Some events for DataWindow controls hcodes that you can set to override the default action that occurs when the event istriggered. The codes and the ir meaning depend on the event. In PowerBuilder, youthe code with a RETURN statement. In the Web ActiveX, you call the SetActionCodesetActionCode method.

  • 5/22/2018 Manejo de Erorres - Handling DataWindow Errors - PowerBuilder 9

    http:///reader/full/manejo-de-erorres-handling-datawindow-errors-powerbuilde

    Example

    PowerBuilderHere is a sample script for the DBError event:

    // Database error -195 means that some of the

    // required values are missing

    IF sqldbcode = -195 THEN

    MessageBox("Missing Information", &

    "You have not supplied values for all " &

    +"the required fields.")

    END IF

    // Return code suppresses default message box

    RETURN 1

    During execution, the user would see the following message box after the error:

    Web ActiveXIn JavaScript, the code for the DBError event might look like this:

    // Database error -195 means that some of the

    // required values are missing

    if (sqldbcode == -195) {

    alert("Missing information:\n" +

    "You have not supplied values for all " +

    "the required fields.");

    }

    // Action code suppresses default message box

    dw_1.SetActionCode(1);

    Errors in property and data expressions and the Err

    event

    A DataWindow control's Error event is triggered whenever an error occurs in a dataproperty expression at execution time. These express ions that refer to data andproperties of a DataWindow object may be valid under some execution time conditibut not others. The Error event allows you to respond with error recovery logic whean expression is not valid.

    PowerBuilder compiler information

  • 5/22/2018 Manejo de Erorres - Handling DataWindow Errors - PowerBuilder 9

    http:///reader/full/manejo-de-erorres-handling-datawindow-errors-powerbuilde

    In PowerBuilder, when you use a data or property expression, the PowerScript comchecks the syntax only as far as the Object property. Everything following the Objeproperty is evaluated at execution time. For example, in the following express ion, tcolumn name emp_name and the property Visible are not checked until execution t

    dw_1.Object.emp_name.Visible = "0"

    If the emp_name column did not exist in the DataWindow, or if you had misspelled property name, the compiler would not detect the error. However, at execution timPowerBuilder would trigger the DataWindow control's Error event.

    Using a Try-Catch block

    The Error event is triggered even if you have surrounded an error producing data oproperty expression in a Try-Catch block. The catch statement is executed after theError event is triggered, but only if you do not code the Error event or do not changthe default Error event action from ExceptionFail!. The following example shows aproperty expression in a Try-Catch block:

    TRY

    dw_1.Object.emp_name.Visible = "0"

    CATCH (dwruntimeerror dw_e)

    MessageBox ("DWRuntimeError", dw_e.text)

    END TRY

    Determining the cause of the error

    The Error event has several arguments that provide information about the errorcondition. You can check the values of the a rguments to determine the cause of theerror. For example, you can obtain the internal error number and error text, the naof the object whose script caused the error, and the full text of the script where theerror occurred. The information provided by the Error event's arguments can be helin debugging expressions that are not checked by the compiler.

    If you catch a DWRuntimeError error, you can use the properties of that class insteathe Error event arguments to provide information about the error condition. Thefollowing table displays the correspondences between the Error event arguments athe DWRuntimeError properties.

    Table 2-6: Correspondence between Error eventarguments and DWRuntimeError properties

    Error event argument DWRuntimeError property

    errornumber number

    errorline line

    errortext text

    errorw indowmenu objectname

    errorobject class

    errorscript routinename

    Controlling the outcome of the event

  • 5/22/2018 Manejo de Erorres - Handling DataWindow Errors - PowerBuilder 9

    http:///reader/full/manejo-de-erorres-handling-datawindow-errors-powerbuilde

    When the Error event is triggered, you can have the application ignore the error ancontinue processing, substitute a different return value, or escalate the error bytriggering the SystemError event. In the Error event, you can set two argumentspassed by reference to control the outcome of the event.

    Table 2-7: Setting arguments in the Error event

    Argument Description

    Action

    A value you specify to control the application's course of action as aresult of the error. Values are:

    ExceptionIgnore!ExceptionSubstituteReturnValue!ExceptionFail! (default action)

    ReturnValueA value whose data type matches the expected value that theDataWindow would have returned. This value is used when the valuof action is ExceptionSubstituteReturnValue!.

    For a complete description of the arguments of the Error event, see the DataWindowReference.

    When to substitute a return value The ExceptionSubstituteReturnValue! actionallows you to substitute a return value when the last element of an expression cauan error. Do not use ExceptionSubstituteReturnValue! to substitute a return valuewhen an element in the middle of an express ion causes an error.

    The ExceptionSubstituteReturnValue! action is most useful for handling errors in daexpressions.