InterSystems FAQ rubric
If you want to raise an arbitrary custom error in a TRY block, you can pass an exception with a throw as follows. In the following example, a custom error is raised if Stcount is less than 1.
Class User.Test
{
ClassMethod ExceptionTest()
{
try
{
if (Stcount<1) {
throw ##class(%Exception.General).%New("User-defined error", "5001", "location", "Data at location error")
}
}
catch ex
{
write "Errors #", ex.Code, ": ", ex.Name, " : ", ex.Location, " ", ex.Data
return
}
}
}
In the above example, if Stcount is less than 1, an error like the following will be output:
USER>do ##class(User.Test).ExceptionTest()
Error #5001: User-defined error: Data at location error
For more information, see the following documentation:
ObjectScript command _THROW
If you want to create an arbitrary status code, do the following:
USER>set st = ##class(%SYSTEM.Status).Error(5001,"This is a user-defined error")
USER>zwrite st
st="0 "_$lb($lb(5001,"This is a user-defined error",,,,,,,,$lb(,"USER",$lb("e^zError+1^%SYSTEM.Status.1^1","e^^^0"))))
USER>do $SYSTEM.Status.DisplayError(st)
Error #5001: This is a user-defined error