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;
Bookmark/Search this post with:
Comments
Post new comment