
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
59 lines
1.7 KiB
Plaintext
59 lines
1.7 KiB
Plaintext
== Why is this an issue?
|
|
|
|
In Python 3, attempting to raise an object which does not derive from BaseException will raise a ``++TypeError++``. In Python 2 it is possible to raise old-style classes but this shouldn't be done anymore in order to be compatible with Python 3.
|
|
|
|
|
|
If you are about to create a custom Exception class, note that custom exceptions should inherit from ``++Exception++``, not ``++BaseException++``. ``++Exception++`` allows people to catch all exceptions except the ones explicitly asking the interpreter to stop, such as ``++KeyboardInterrupt++`` and https://docs.python.org/3/library/exceptions.html#GeneratorExit[``++GeneratorExit++``] which is not an error. See https://www.python.org/dev/peps/pep-0352/#exception-hierarchy-changes[PEP 352] for more information.
|
|
|
|
|
|
This rule raises an issue when an object which doesn't derive from BaseException is raised.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,python]
|
|
----
|
|
raise "Something went wrong" # Noncompliant
|
|
|
|
class A:
|
|
pass
|
|
|
|
raise A # Noncompliant
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,python]
|
|
----
|
|
class MyError(Exception):
|
|
pass
|
|
|
|
raise MyError("Something went wrong")
|
|
raise MyError
|
|
----
|
|
|
|
|
|
== Resources
|
|
|
|
* https://docs.python.org/3/tutorial/errors.html[Python documentation - Errors and Exceptions]
|
|
* https://www.python.org/dev/peps/pep-0352/#exception-hierarchy-changes[PEP 352 - Required Superclass for Exceptions]
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Change this code so that it raises an object deriving from BaseException.
|
|
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::../comments-and-links.adoc[]
|
|
endif::env-github,rspecator-view[]
|