Try OpenEdge Now
skip to main content
Error Handling
Raising errors with THROW : THROW with user-defined functions : Throwing system errors
 

Throwing system errors

With structured error handling, you can raise error in the caller by throwing an error from a CATCH block on the main function block. A CATCH block in the caller can then handle system error, as shown:
/* First technique for returning a system error from a user-defined function. */
DEFINE VARIABLE FuncReturn AS LOGICAL NO-UNDO.

FUNCTION ReturnSysError RETURNS LOGICAL:
FIND FIRST Customer WHERE CustNum = 1000.

CATCH mySysError AS Progress.Lang.SysError:
UNDO, THROW mySysError.
END CATCH.

END FUNCTION.

ASSIGN FuncReturn = ReturnSysError().

CATCH mySysError AS Progress.Lang.SysError:
DISPLAY "Error message returned from function: "
mySysError:GetMessage(1) FORMAT "X(60)" SKIP.
END CATCH.
You might want to upgrade your functions to use this technique without upgrading the traditional error handling used in the caller. You can use the NO-ERROR option on the ASSIGN statement of the target variable. The AVM automatically moves the information found in the SysError object to the ERROR-STATUS system handle. For example:
/* Second technique for returning a system error from a user-defined function.
*/

DEFINE VARIABLE FuncReturn AS LOGICAL NO-UNDO.

FUNCTION ReturnSysError RETURNS LOGICAL:
FIND FIRST Customer WHERE CustNum = 1000.

CATCH mySysError AS Progress.Lang.SysError:
UNDO, THROW mySysError.
END CATCH.

END FUNCTION.

ASSIGN FuncReturn = ReturnSysError() NO-ERROR.

IF ERROR-STATUS:ERROR THEN
DISPLAY "Error message returned from function: "
ERROR-STATUS:Get-Message(1) FORMAT "X(60)".
The ROUTINE-LEVEL ON ERROR UNDO, THROW statement can also be used to change the default error handling for all user-defined functions in a procedure or class file. In the following example, notice that when the FIND fails the presence of the implicit THROW suppresses display of the error message at the statement level:
/* Third technique for returning a system error from a user-defined function.
*/

ROUTINE-LEVEL ON ERROR UNDO, THROW.

DEFINE VARIABLE FuncReturn AS LOGICAL NO-UNDO.

FUNCTION ReturnSysError RETURNS LOGICAL:

FIND FIRST Customer WHERE CustNum = 1000.

END FUNCTION.

ASSIGN FuncReturn = ReturnSysError().

CATCH mySysError AS Progress.Lang.SysError:
DISPLAY "Error message returned from function: "
mySysError:GetMessage(1) FORMAT "X(60)" SKIP.
END CATCH.