Skip to main content

Exception Handling

Exception Handling

Any COM object may raise an exception as the result of some operation, be it a method call or a property set/get. When an exception is raised, the exception is propagated into Caché via the ZTrap mechanism. The calling code will receive an error with the error code <ZACTX> and the local variable %objlasterror will contain a complete textual description of the error. Programmers should plan for this error and take action accordingly.

Example: Exception Handling

Here is an example of using a COM object which retrieves files by FTP. The object is created and the CurrentDirectory property is queried. The COM object throws an exception because it is not valid to try to determine the current directory until the FTP connection has been made. We will try this from a Caché command line (terminal session):

 Set obj = ##class(Activate.RETRIEVERLib.FtpRetriever).%New()
 Write obj.CurrentDirectory

In this case, this will throw an error:

<ZACTX>CurrentDirectoryGet+4^Activate.RETRIEVERLib.FtpRetriever.1

The error code associated with the <ZATCX> error should be in the local variable %objlasterror. We can retrieve the complete text of the error message by calling $system.OBJ.DisplayError:

 Do $system.OBJ.DisplayError(%objlasterror)

Which will result in the following output:

ERROR #1101: Com Exception: '-2147220888 Ftp Retriever Connection must
be established before attempting this operation'
FeedbackOpens in a new tab