Macaulay2 » Documentation
Packages » Macaulay2Doc » The Macaulay2 language » error handling » trap
next | previous | forward | backward | up | index | toc

trap -- trap an error

Description

The code c is evaluated and a sequence containing two elements is returned. If the evaluation completes successfully, then the first element is the value and the second element is null.

i1 : trap 5

o1 = (5, )

o1 : Sequence

If an error occurs, then the first element is null and the second element is an Error object containing information about the error is returned.

i2 : trap 1/0

o2 = (, division by zero)

o2 : Sequence

Note that this is a Keyword, not a method, and so it is not necessary to enclose c in parentheses.

i3 : trap 5 == "foo"

o3 = (, no method for binary operator == applied to objects:)
                    5 (of class ZZ)
             ==     "foo" (of class String)

o3 : Sequence
i4 : {trap error "bar", 1/2}

               1
o4 = {(, bar), -}
               2

o4 : List
i5 : trap 1/0; 1/2

     1
o6 = -
     2

o6 : QQ

See also

For the programmer

The object trap is a keyword.


The source of this document is in /build/reproducible-path/macaulay2-1.26.05+ds/M2/Macaulay2/packages/Macaulay2Doc/ov_debugging.m2:397:0.