Skip to content

Commit dad4750

Browse files
committed
Document and add tests for "attempt" and "Error"
1 parent 9353414 commit dad4750

File tree

4 files changed

+80
-2
lines changed

4 files changed

+80
-2
lines changed

M2/Macaulay2/packages/Macaulay2Doc/functions/locate-doc.m2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Node
1616
(locate, Package)
1717
(locate, List)
1818
(locate, ZZ)
19+
(locate, Error)
1920
Headline
2021
locate source code
2122
Usage

M2/Macaulay2/packages/Macaulay2Doc/ov_debugging.m2

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ document {
293293
}
294294

295295
document {
296-
Key => error,
296+
Key => {error, (error, Thing), (error, Error), (error, String)},
297297
Headline => "deliver error message",
298298
Usage => "error s",
299299
Inputs => {
@@ -327,6 +327,73 @@ document {
327327
}
328328
}
329329

330+
doc ///
331+
Key
332+
Error
333+
(NewFromMethod, Error, String)
334+
(NewFromMethod, Error, Thing)
335+
Headline
336+
error information
337+
Description
338+
Text
339+
An instance of this class is returned as the second element when
340+
@TO symbol attempt@ catches an error. The error message may be recovered
341+
using @TO toString@ and the location using @TO locate@.
342+
Example
343+
(val, err) = attempt 1/0
344+
toString err
345+
locate err
346+
Text
347+
An @CODE "Error"@ object may be constructed from an error message.
348+
Example
349+
err = new Error from "foo"
350+
Text
351+
An error may be raised from an @CODE "Error"@ object using @TO error@.
352+
Example
353+
stopIfError = false
354+
error err
355+
Text
356+
It is possible to create subclasses of @CODE "Error"@ for finer error
357+
handling.
358+
Example
359+
MyError = new SelfInitializingType of Error
360+
(val, err) = attempt error MyError "bar"
361+
err
362+
SeeAlso
363+
symbol attempt
364+
error
365+
///
366+
367+
doc ///
368+
Key
369+
symbol attempt
370+
Headline
371+
attempt to evaluate code and get information about any errors that occur
372+
Usage
373+
attempt c
374+
Description
375+
Text
376+
The code @VAR "c"@ is evaluated and a sequence containing two elements
377+
is returned. If the evaluation completes successfully, then the first
378+
element is the value and the second element is null.
379+
Example
380+
attempt 5
381+
Text
382+
If an error occurs, then the first element is null and the second element
383+
is an @TO Error@ object containing information about the error is
384+
returned.
385+
Example
386+
attempt 1/0
387+
Text
388+
Note that this is a @TO Keyword@, not a method, and so it is not
389+
necessary to enclose @VAR "c"@ in parentheses.
390+
Example
391+
attempt 5 == "foo"
392+
{attempt error "bar", 1/2}
393+
attempt 1/0; 1/2
394+
SeeAlso
395+
symbol try
396+
///
330397

331398
document {
332399
Key => "recursionLimit",

M2/Macaulay2/packages/Macaulay2Doc/ov_language.m2

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,8 @@ document {
704704
TO "error",
705705
TO "try",
706706
TO "throw",
707+
TO "attempt",
708+
TO "Error",
707709
}
708710
}
709711

@@ -733,7 +735,8 @@ document {
733735
"The behavior of interrupts (other than alarms) is unaffected.",
734736
EXAMPLE "apply(-3..3,i->try 1/i else infinity)",
735737
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
736-
recommended to use this function sparingly, if at all."
738+
recommended to use this function sparingly, if at all.",
739+
SeeAlso => {"attempt"},
737740
}
738741

739742
document {

M2/Macaulay2/tests/normal/error-messages.m2

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
(val, err) = attempt error "foo"
2+
assert Equation(toString err, "foo")
3+
4+
MyError = new SelfInitializingType of Error
5+
(val, err) = attempt error MyError "foo"
6+
assert instance(err, MyError)
7+
18
stderr << "--testing the error messages must be done manually" << endl
29
end
310

0 commit comments

Comments
 (0)