Kbase P173073: 4GL/ABL: How to THROW and CATCH an error object?
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  16/09/2010 |
|
Status: Unverified
GOAL:
4GL/ABL: How to THROW and CATCH an error object?
GOAL:
How to THROW and CATCH a Progress.Lang.AppError in a class method?
GOAL:
Sample code to demonstrate CATCHing an error thrown inside a DO block of a class method?
GOAL:
Sample code demonstrate CATCHing an error thrown inside a DO TRANSACTION block of a class method?
FACT(s) (Environment):
All Supported Operating Systems
OpenEdge 10.2B
FIX:
In general, an error may be thrown using the THROW option of the DO ON ERROR statement. For example:
DO ON ERROR UNDO, THROW:
/* The following line raises error (138) which is diverted to a Progress.Lang.SysError object and thrown to the main block. */
FIND Customer 1000.
END.
CATCH eAnyError AS Progress.Lang.Error:
MESSAGE
"Error Number:~t" eAnyError:GetMessageNum(1) "~n"
"Error Text:~t" eAnyError:GetMessage(1)
VIEW-AS ALERT-BOX BUTTONS OK TITLE "Error processing in the CATCH for mainprocedure block".
END CATCH.
Also, an error may be thrown using the ROUTINE-LEVEL ON ERROR UNDO, THROW statement. For example:
ROUTINE-LEVEL ON ERROR UNDO, THROW.
DO:
FIND FIRST Customer WHERE Customer.CustNum = 99999.
END.
CATCH eAnyError AS Progress.Lang.SysError:
MESSAGE "This CATCH block is associated with the the main block and handles the errors generated in it."
VIEW-AS ALERT-BOX BUTTONS OK.
END CATCH.
Finally, an error may also be thrown using the UNDO statement. For example:
FIND Customer 1000 NO-ERROR.
IF ERROR-STATUS:ERROR THEN
UNDO, THROW NEW Progress.Lang.AppError("Can't find this customer", 550).
CATCH eAnyError AS Progress.Lang.AppError:
MESSAGE
"Error Number:~t" eAnyError:GetMessageNum(1) "~n"
"Error Text:~t" eAnyError:GetMessage(1)
VIEW-AS ALERT-BOX BUTTONS OK TITLE "Processing of the error thrown by the UNDO, THROW statement".
END CATCH.
The following sample code demonstrates CATCHing an error thrown inside a DO block of a class method:
CLASS CatchDoBlockError:
CONSTRUCTOR PUBLIC CatchDoBlockError ( ):
SUPER ().
END CONSTRUCTOR.
DESTRUCTOR PUBLIC CatchDoBlockError ( ):
END DESTRUCTOR.
METHOD PUBLIC VOID TestMethod(OUTPUT cMessage AS CHARACTER):
DO:
UNDO , THROW NEW Progress.Lang.AppError ("DO Block AppError", 1).
END.
CATCH e AS Progress.Lang.Error:
cMessage = e:ToString() + "~n" + e:GetMessage(1).
END CATCH.
END METHOD.
END CLASS.
The following sample code demonstrates CATCHing an error thrown inside a DO TRANSACTION block of a class method
CLASS CatchDoTransactionError:
CONSTRUCTOR PUBLIC CatchDoTransactionError ( ):
SUPER ().
END CONSTRUCTOR.
DESTRUCTOR PUBLIC CatchDoTransactionError ( ):
END DESTRUCTOR.
METHOD PUBLIC VOID ErrorMethod(OUTPUT cMessage AS CHARACTER):
DO TRANSACTION:
UNDO , THROW NEW Progress.Lang.AppError ("DO TRAN.SACTION Block AppError", 1).
END.
CATCH e AS Progress.Lang.Error:
cMessage = e:ToString() + "~n" + e:GetMessage(1).
END CATCH.
END METHOD.
END CLASS..