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

# try -- catch an error

- Usage:
    `try x then y else z`
- Inputs:
  - x, code
  - y, code
  - z, code
- Consequences:
  - the code x is run; if no error or alarm occurs, then the code y is run; otherwise, the code z is run

## Description

The return value is the value returned by y or z, as the case may be.

The clause 'then y' may be omitted, in which case the return value is the value returned by x, if there is no error or alarm.

The clause 'else z' may be omitted, in which case the return value is the value returned by y, unless an error or alarm occurs, in which case null is returned.

The clauses 'then y else z' may both be omitted, in which case the return value is the value returned by x, unless an error or alarm occurs, in which case null is returned.

The behavior of interrupts (other than alarms) is unaffected.

    i1 : apply(-3..3,i->try 1/i else infinity)

            1    1                   1  1
    o1 = (- -, - -, -1, infinity, 1, -, -)
            3    2                   2  3

    o1 : Sequence

## Caveat

We will change the behavior of this function soon so that it will be possible to catch errors of a particular type. Meanwhile, users are recommended to use this function sparingly, if at all.

## For the programmer

The object try is a keyword.

The source of this document is in /build/macaulay2-88fgJW/macaulay2-1.25.11+ds/M2/Macaulay2/packages/Macaulay2Doc/ov_language.m2:738:0.