Skip to content

Commit e0f2f60

Browse files
committed
Add tips for choosing an exception
1 parent 0c390a0 commit e0f2f60

File tree

1 file changed

+40
-5
lines changed

1 file changed

+40
-5
lines changed

src/doc/en/developer/coding_in_python.rst

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ scratch. Try to figure out how your code should fit in with other Sage
6464
code, and design it accordingly.
6565

6666

67-
Special sage functions
67+
Special Sage functions
6868
======================
6969

7070
Functions with leading and trailing double underscores ``__XXX__`` are
@@ -103,8 +103,8 @@ You should implement the ``_latex_`` and ``_repr_`` method for every
103103
object. The other methods depend on the nature of the object.
104104

105105

106-
LaTeX Representation
107-
--------------------
106+
LaTeX representation
107+
====================
108108

109109
Every object ``x`` in Sage should support the command ``latex(x)``, so
110110
that any Sage object can be easily and accurately displayed via
@@ -158,7 +158,7 @@ typeset version of this.
158158

159159

160160
Print representation
161-
--------------------
161+
====================
162162

163163
The standard Python printing method is ``__repr__(self)``. In Sage,
164164
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
194194
195195
196196
Matrix or vector from object
197-
----------------------------
197+
============================
198198

199199
Provide a ``_matrix_`` method for an object that can be coerced to a
200200
matrix over a ring `R`. Then the Sage function ``matrix`` will work
@@ -473,6 +473,41 @@ example:
473473
Note that the syntax in ``except`` is to list all the exceptions that
474474
are caught as a tuple, followed by an error message.
475475

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+
476511

477512
Integer return values
478513
=====================

0 commit comments

Comments
 (0)