An exception can be thrown from any code block or subroutine using the PSL throw statement. There are two ways to throw an error object.
To create and throw an error, use the following syntax:
throw Class.new("Error","errorType,errorDescription,errorContext")
where:
Error |
The string "Error", as that is currently the only "throwable" class. |
errorType |
An error type defined in the Error system table (STBLER). The default is ETUNDEF. |
errorDescription |
Optional free-form text description. |
errorContext |
Optional error-specific context that may be useful to the calling program. |
To re-throw an existing error object, use the following syntax:
throw Error
If a catch statement has an optional exception type trap condition, the trap is limited to exceptions of that type. The scope of any previous catch statement handles all other exceptions.
If an exception is thrown from inside a catch block, it is passed to the catch block in effect at the previous stack exception (to prevent infinite loops). The throw command implicitly quits from the current block and passes an error object to the calling block or subroutine.
Example #1
catch Error {
set ET=Error.type
set CONTEXT=Error.context
if ET["%GTM" do ZE^UTLERR quit
set RM=Error.description
set ET=ET_"-"_RM_"-"_Error.thrownAt
do ^UTLERR
}
if 'Db.isDefined("DEP",":CID") ...
throw Class.new("Error","INVLDADT,Invalid account")
else type RecordDEP dep=Db.getRecord(“DEP”,”:CID”)
Example #2
This example illustrates how to throw and catch errors between main programs and subroutines (i.e., at multiple stack levels).
main // Main subroutine
catch xyz {
write !,"Tag main:",xyz.type," Thrown at:",xyz.thrownAt
}
do subr
type RecordDEP dep = Db.getRecord("DEP",1212)
quit
subr //
catch error{
write !,"Tag subr:",error.type," Thrown at: ",error.thrownAt
throw MyError
}
if x // will generate %GTM_E_UNDEF
quit
Results
D ^T
Tag subr: %GTM_E_UNDEF Thrown at: subr+4^T
Tag main: MyError Thrown at: vtrap2+6^T
%GTM-I-BREAK, Break instruction encountered
At M source location +1^SCADMOD