Try and Catch Built-in Function

Your rating: None Average: 4.5 (2 votes)

This is a simple example on when and how to use the Try and Catch built-in function. The code below will fetch a field value from a scroll area on a page and delete the row depending on the value. If the value is >= 0 or if it is a "charachter" value, then go ahead and mark the row for deletion.

See code comments to get a better idea of what the code is doing.

Local number &someValue;
Local string &strSomeValue;
Local Rowset &RS;

/*fetch result set;*/
&RS = GetLevel0()(1).GetRowset(Scroll.scroll_table);
&RS.Flush();

/* notice the Setp -1 in the "for" loop.  I'm performing a row delete so need to start from the last row in the buffer back up */
   For &I = &RS.ActiveRowCount To 1 Step - 1
    /*using try and catch to determine if value in scroll_field is a number or 'N/A'.*/
     /*if it is a number then assign it to &someValue, otherwise assign it to &strSomeValue*/
      try
         /*Here i'm trying to assign the fetched value to  &someValue without checking if the value is a number or a string.  The try and catch will do that for me! */
         &someValue = FetchValue(Scroll.scroll_table, &I, scroll_field);
         If &someValue >= 0 Then
            /*Just an FYI, if your scroll recrod is a view, no records will be deleted from the database.*/
            &RS.DeleteRow(&I);
         End-If;
         
      catch Exception &excepMessage
        /*If the value fetched is a character, an exception will be thrown as i was trying to assign it to a number from the try above! */
         rem -- MessageBox(0, "", 0, 0, "Caught exception: " | &excepMessage.ToString());
/*Now assign the value to a string*/
         &strSomeValue = FetchValue(Scroll.scroll_table, &I, scroll_field);
         &RS.DeleteRow(&I);
         rem -- MessageBox(0, "", 0, 0, "&strSomeValue: " | &strSomeValue);
         
      end-try;
     
   End-For;

Please try to help out with unanswered topics on the forum. Chances are you have had the same issue/question some time in your IT career!

Comments

Have a question? Please ask it on the forum instead.

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
The question below is to prevent automated spam submissions.
11 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.