Written by

Principal Integration Architect | Cloud Computing | Data Science | AI & ML | CSM
Article Neerav Verma · Sep 27, 2019 2m read

Error Handling Techniques : Tips & Tricks

At least three different ways to process errors (status codes, exceptions, SQLCODE etc is given in ObjectScript. Most systems have status, but for a range of reasons exceptions are more convenient to manage. You spend some time translating between the various techniques dealing with legacy code. For reference, I use several of these excerpts. It is hoped that they will also support others.

///Status from SQLCODE:set st = $$$ERROR($$$SQLError, SQLCODE, $g(%msg))  //embedded SQLset st = $$$ERROR($$$SQLError, rs.%SQLCODE, $g(rs.%Message)) //dynamic SQL///Exception from SQLCODE:throw ##class(%Exception.SQL).CreateFromSQLCODE(SQLCODE,%msg) //embedded SQLthrow ##class(%Exception.SQL).CreateFromSQLCODE(rs.%SQLCODE,rs.%Message) //dynamic SQLthrow:(SQLCODE'=0)&&(SQLCODE'=100) ##class(%Exception.SQL).CreateFromSQLCODE(SQLCODE,%msg) //don't throw if query succeeds or finds no data ///Exception from status:$$$ThrowOnError(st)///Status from exception:set st = err.AsStatus()///Creating a custom error status:set st = $$$ERROR($$$GeneralError,"Custom error message")///Throwing a custom exception:$$$ThrowStatus($$$ERROR($$$GeneralError,"Custom error message"))///Handling a SOAP error with a status:try {  //SOAP request code} Catch err {  If err.Name["ZSOAP" {    Set st = %objlasterror  } Else {    Set st = err.AsStatus()  }}return st///Defining a custom exception classClass App.Exceptions.SomeException Extends %Exception.AbstractException{Method OnAsStatus() As %Status{  return $$$ERROR($$$GeneralError,"Custom error message")}}///Throwing and catching a custom exceptiontry {  throw ##class(App.Exceptions.SomeException).%New()} catch err {  if err.%ClassName(1) = ##class(App.Exceptions.SomeException).%ClassName(1) {    //some handling unique to this type of exception  }}

Comments

Paul Osbeck · Sep 30, 2019

The way I've done this before was to create a JDBC Gateway Connection. And I had to add a business service to the production for a Java Gateway Service. This needed to be in place in order for my JDBC connection to actually work.

0