@@ -64,7 +64,7 @@ scratch. Try to figure out how your code should fit in with other Sage
64
64
code, and design it accordingly.
65
65
66
66
67
- Special sage functions
67
+ Special Sage functions
68
68
======================
69
69
70
70
Functions with leading and trailing double underscores ``__XXX__ `` are
@@ -103,8 +103,8 @@ You should implement the ``_latex_`` and ``_repr_`` method for every
103
103
object. The other methods depend on the nature of the object.
104
104
105
105
106
- LaTeX Representation
107
- --------------------
106
+ LaTeX representation
107
+ ====================
108
108
109
109
Every object ``x `` in Sage should support the command ``latex(x) ``, so
110
110
that any Sage object can be easily and accurately displayed via
@@ -158,7 +158,7 @@ typeset version of this.
158
158
159
159
160
160
Print representation
161
- --------------------
161
+ ====================
162
162
163
163
The standard Python printing method is ``__repr__(self) ``. In Sage,
164
164
that is for objects that derive from ``SageObject `` (which is
@@ -194,7 +194,7 @@ Here is an example of the ``_latex_`` and ``_repr_`` functions for the
194
194
195
195
196
196
Matrix or vector from object
197
- ----------------------------
197
+ ============================
198
198
199
199
Provide a ``_matrix_ `` method for an object that can be coerced to a
200
200
matrix over a ring `R `. Then the Sage function ``matrix `` will work
@@ -473,6 +473,41 @@ example:
473
473
Note that the syntax in ``except `` is to list all the exceptions that
474
474
are caught as a tuple, followed by an error message.
475
475
476
+ If you are writing new code to Sage, you soon face the problem of choosing an
477
+ exception most suitable for an errorneous situation. This is often a tricky
478
+ business. The following aims to guide you in choosing from the most relevant
479
+ exceptions to Sage developers. A method (or a function) accepts input described
480
+ in the ``INPUT `` block of :ref: `the docstring <section-docstring-function >` and
481
+ returns output. You may raise one of
482
+
483
+ - `TypeError <https://docs.python.org/3/library/exceptions.html#TypeError >`_:
484
+ if the input belongs to a class of objects that are not supported by the
485
+ method. For example, a method works only with monic polynomials over a
486
+ finite field, but a polynomial over rationals was given.
487
+
488
+ - `ValueError <https://docs.python.org/3/library/exceptions.html#ValueError >`_:
489
+ if the input has a value not supported by the method. For example, the above
490
+ method was given a non-monic polynomial.
491
+
492
+ - `ArithmeticError
493
+ <https://docs.python.org/3/library/exceptions.html#ArithmeticError> `_: if the
494
+ method performs an arithmetic operation (sum, product, quotient, and the
495
+ like) but the input is not appropriate.
496
+
497
+ - `ZeroDivisionError
498
+ <https://docs.python.org/3/library/exceptions.html#TypeError> `_: if the
499
+ method performs division but the input is zero. Note that for non-invertible
500
+ input values, ``ArithmeticError `` is more appropriate.
501
+
502
+ - `NotImplementedError
503
+ <https://docs.python.org/3/library/exceptions.html#NotImplementedError> `_: if
504
+ the input is for a feature not yet implemented by the method.
505
+
506
+ If no specific error seems to apply for your situation, `RuntimeError
507
+ <https://docs.python.org/3/library/exceptions.html#RuntimeError> `_ can be used.
508
+ In all cases, the string associated with the exception should describe the
509
+ details of what went wrong.
510
+
476
511
477
512
Integer return values
478
513
=====================
0 commit comments