Throwing Exceptions

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.

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.

 

throw Error

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