ItemNotPresent class

From m204wiki
Jump to: navigation, search


An ItemNotPresent exception indicates that an explict or implicit NamedArraylist Item method requested a value not present on the NamedArrayList. NamedArraylist here can mean the NamedArraylist class, FloatNamedArraylist class, or UnicodeNamedArraylist class.

This exception class has no properties. It is simply a notification that requested item was not present on the NamedArrayList.

The class's only method is the New constructor, which you would typically use with a User Language Throw statement to produce an ItemNotPresent exception yourself. For example:

throw %(itemNotPresent):new

Remember that you catch an exception with the Catch statement; if an exception condition occurs outside a Catch for it, the request is cancelled. For example, if you think it is possible that a value is not on the NamedArraylist, you can do:

try %value = %valueList(%string) ... process the value catch itemNotPresent end try

On the other hand, if you always expect the item to be present, you might want a program crash to result if you are wrong about the match, so you could leave out a Try/Catch.

The Try/Catch approach is slightly more efficient than testing for a zero result with the Number (NamedArraylist function) function, Number (FloatNamedArraylist function), or Number (UnicodeNamedArraylist function)). Try generates no quads other than the branch around the Catch block if the item is present. If UseDefault is set to True for the NamedArraylist, an ItemNotPresent exception is never thrown. Using a default value for not-present items is more efficient but less flexible than catching an ItemNotPresent exception.

The ItemNotPresent class is available as of Sirius Mods version 8.0.

The most common use of the ItemNotPresent exception class is in NamedArraylists that are used as a cache for expensive-to-produce values or objects. For example, if %recordsets is a NamedArraylist of Recordset objects (in some file), the following is an example of how one might avoid doing a Find more than once for a specific value:

for %i from 1 to %keys:count  %key = %key(%i) try %recordset = %recordsets(%key) catch itemNotPresent find records to %recordset key = %key end find  %recordsets(%key) = %recordset end try end for

The ItemNotPresent methods

MethodDescription
NewCreate a new ItemNotPresent object

The methods in the class are described in the subsections that follow. In addition:


New constructor

Create a new ItemNotPresent object (ItemNotPresent class)

[Introduced in Sirius Mods 8.0]

This method generates an instance of an ItemNotPresent exception. The New method format follows:

Syntax

%itemNotPresent = [%(ItemNotPresent):]New

Syntax terms

%itemNotPresent An ItemNotPresent %variable which will refer to the newly created object.
[%(ItemNotPresent):] The class name in parentheses denotes a Constructor. See Usage notes, below, for more information about invoking an ItemNotPresentConstructor.

Usage notes

  • As described in Using New or other Constructors,New can be invoked with no object, with an explicit class name, or with an object variable in the class, even if that object is Null:

    %itemnp = new %itemnp = %(ItemNotPresent):new %itemnp = %itemnp:new