Skip to main content

Try/Catch Construct, and Throw Command

You use the Try/Catch construct to monitor a block of code (within Try) for errors at runtime. When an error occurs, the system raises an exception and transfers execution to another block of code (within Catch) to handle the error. Catch has an argument, which is an exception object containing information about the exception: name, code, location, and additional data related to the exception. You use Throw within the Try/Catch construct to explicitly raise an exception.

The classes for exceptions belong to the %Exception package. Example classes are %Exception.SystemExceptionOpens in a new tab for system exceptions, and %Exception.GeneralException for simple custom exceptions. You can also extend %Exception.AbstractExceptionOpens in a new tab and create your own exception classes.

As with the looping constructs, Quit and Return also behave differently when used inside the code block of a Try or Catch.

  • Quit terminates the Try/Catch only.

  • Return terminates the Try/Catch and the current class method, procedure, or routine.

VS Code - ObjectScript


/// examples for ObjectScript Tutorial
Class ObjectScript.Examples
{

/// examples of system and custom exceptions
ClassMethod Exceptions(x as %Numeric)
{
    // UNDEFINED error throws a system exception
    try {
        write "Hello!", !, xyz
    }
    catch err {
        write !, "Error name: ", ?20, err.Name,
              !, "Error code: ", ?20, err.Code,
              !, "Error location: ", ?20, err.Location,
              !, "Additional data: ", ?20, err.Data, !
    }

    // DIVIDE error throws a system exception
    try {
        write 1/0
    }
    catch err {
        write !, "Error name: ", ?20, err.Name,
              !, "Error code: ", ?20, err.Code,
              !, "Error location: ", ?20, err.Location,
              !, "Additional data: ", ?20, err.Data, !
    }
    
    // create a simple custom exception object and throw it
    set ex = ##class(%Exception.General).%New()
    set ex.Name = "Demo Exception",
        ex.Code = 100000,
        ex.Data = "Tutorial Example"
    try {
        write !, "Hello!", !
        if (x >= 5) throw ex  // throw the exception
    }
    catch err {
        write !, "Error name: ", ?20, err.Name,
              !, "Error code: ", ?20, err.Code,
              !, "Error location: ", ?20, err.Location,
              !, "Additional data: ", ?20, err.Data, !
        if (x = 5) return  // terminate method
    }
    write !, "Finished!"
}
}

In this test, the call to Exceptions() is passed 5 in order to trigger the Return, so the last line of code write !, "Finished!" is skipped.

Testing using the Terminal


USER>do ##class(ObjectScript.Examples).Exceptions(5)
Hello!

Error name:         <UNDEFINED>
Error code:         9
Error location:     zExceptions+3^ObjectScript.Examples.1
Additional data:    xyz

Error name:         <DIVIDE>
Error code:         18
Error location:     zExceptions+13^ObjectScript.Examples.1
Additional data:    

Hello!

Error name:         Demo Exception
Error code:         100000
Error location:     
Additional data:    Tutorial Example

USER>
FeedbackOpens in a new tab