Error -> Defect for defects (#13908)

* Error -> Defect for defects

The distinction between Error and Defect is subjective,
context-dependent and somewhat arbitrary, so when looking at an
exception, it's hard to guess what it is - this happens often when
looking at a `raises` list _without_ opening the corresponding
definition and digging through layers of inheritance.

With the help of a little consistency in naming, it's at least possible
to start disentangling the two error types and the standard lib can set
a good example here.
This commit is contained in:
Jacek Sieka 2020-04-28 19:56:01 +02:00 • committed by GitHub
commit 7d6cbf290a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
92 changed files with 323 additions and 300 deletions

View file

@ -451,7 +451,7 @@ Miscellaneous
This module implements a simple logger.
* `segfaults <segfaults.html>`_
Turns access violations or segfaults into a ``NilAccessError`` exception.
Turns access violations or segfaults into a ``NilAccessDefect`` exception.
* `sugar <sugar.html>`_
This module implements nice syntactic sugar based on Nim's macro system.

View file

@ -113,7 +113,7 @@ pragmas_ for details.
Whether a panic results in an exception or in a fatal error is
implementation specific. Thus the following program is invalid; even though the
code purports to catch the `IndexError` from an out-of-bounds array access, the
code purports to catch the `IndexDefect` from an out-of-bounds array access, the
compiler may instead choose to allow the program to die with a fatal error.
.. code-block:: nim
@ -121,7 +121,7 @@ compiler may instead choose to allow the program to die with a fatal error.
let i = 5
try:
a[i] = 'N'
except IndexError:
except IndexDefect:
echo "invalid index"
The current implementation allows to switch between these different behaviors
@ -1033,10 +1033,10 @@ The IEEE standard defines five types of floating-point exceptions:
precision, for example, 2.0 / 3.0, log(1.1) and 0.1 in input.
The IEEE exceptions are either ignored during execution or mapped to the
Nim exceptions: `FloatInvalidOpError`:idx:, `FloatDivByZeroError`:idx:,
`FloatOverflowError`:idx:, `FloatUnderflowError`:idx:,
and `FloatInexactError`:idx:.
These exceptions inherit from the `FloatingPointError`:idx: base class.
Nim exceptions: `FloatInvalidOpDefect`:idx:, `FloatDivByZeroDefect`:idx:,
`FloatOverflowDefect`:idx:, `FloatUnderflowDefect`:idx:,
and `FloatInexactDefect`:idx:.
These exceptions inherit from the `FloatingPointDefect`:idx: base class.
Nim provides the pragmas `nanChecks`:idx: and `infChecks`:idx: to control
whether the IEEE exceptions are ignored or trap a Nim exception:
@ -1045,12 +1045,12 @@ whether the IEEE exceptions are ignored or trap a Nim exception:
{.nanChecks: on, infChecks: on.}
var a = 1.0
var b = 0.0
echo b / b # raises FloatInvalidOpError
echo a / b # raises FloatOverflowError
echo b / b # raises FloatInvalidOpDefect
echo a / b # raises FloatOverflowDefect
In the current implementation ``FloatDivByZeroError`` and ``FloatInexactError``
are never raised. ``FloatOverflowError`` is raised instead of
``FloatDivByZeroError``.
In the current implementation ``FloatDivByZeroDefect`` and ``FloatInexactDefect``
are never raised. ``FloatOverflowDefect`` is raised instead of
``FloatDivByZeroDefect``.
There is also a `floatChecks`:idx: pragma that is a short-cut for the
combination of ``nanChecks`` and ``infChecks`` pragmas. ``floatChecks`` are
turned off as default.
@ -1620,7 +1620,7 @@ An example:
# accessing n.thenPart is valid because the ``nkIf`` branch is active:
n.thenPart = Node(kind: nkFloat, floatVal: 2.0)
# the following statement raises an `FieldError` exception, because
# the following statement raises an `FieldDefect` exception, because
# n.kind's value does not fit and the ``nkString`` branch is not active:
n.strVal = ""
@ -4063,7 +4063,7 @@ Example:
var a = readLine(f)
var b = readLine(f)
echo "sum: " & $(parseInt(a) + parseInt(b))
except OverflowError:
except OverflowDefect:
echo "overflow!"
except ValueError:
echo "could not convert string to integer"
@ -4226,7 +4226,7 @@ the ``raise`` statement is the only way to raise an exception.
.. XXX document this better!
If no exception name is given, the current exception is `re-raised`:idx:. The
`ReraiseError`:idx: exception is raised if there is no exception to
`ReraiseDefect`:idx: exception is raised if there is no exception to
re-raise. It follows that the ``raise`` statement *always* raises an
exception.

View file

@ -126,7 +126,7 @@ The syntax for type conversions is ``destination_type(expression_to_convert)``
proc getID(x: Person): int =
Student(x).id
The ``InvalidObjectConversionError`` exception is raised if ``x`` is not a
The ``InvalidObjectConversionDefect`` exception is raised if ``x`` is not a
``Student``.
@ -160,7 +160,7 @@ An example:
condition, thenPart, elsePart: Node
var n = Node(kind: nkFloat, floatVal: 1.0)
# the following statement raises an `FieldError` exception, because
# the following statement raises an `FieldDefect` exception, because
# n.kind's value does not fit:
n.strVal = ""
@ -388,7 +388,7 @@ The ``try`` statement handles exceptions:
let a = readLine(f)
let b = readLine(f)
echo "sum: ", parseInt(a) + parseInt(b)
except OverflowError:
except OverflowDefect:
echo "overflow!"
except ValueError:
echo "could not convert string to integer"
@ -443,7 +443,7 @@ instance, if you specify that a proc raises ``IOError``, and at some point it
prevent that proc from compiling. Usage example:
.. code-block:: nim
proc complexProc() {.raises: [IOError, ArithmeticError].} =
proc complexProc() {.raises: [IOError, ArithmeticDefect].} =
...
proc simpleProc() {.raises: [].} =

View file

@ -271,7 +271,7 @@ written.
result = quote do:
if not `arg`:
raise newException(AssertionError,$`lhs` & `op` & $`rhs`)
raise newException(AssertionDefect,$`lhs` & `op` & $`rhs`)
let a = 1
let b = 2
@ -287,7 +287,7 @@ used to get this output.
.. code-block:: nim
if not (a != b):
raise newException(AssertionError, $a & " != " & $b)
raise newException(AssertionDefect, $a & " != " & $b)
With Power Comes Responsibility
-------------------------------