Exceptions handling
Posted on Sat 17 October 2020 in Developer
In order to find and resolve bugs in the source code we are required to follow certain coding conduct when developing software.
- It is forbidden to catch all exceptions by using, catch-everything kind of try/catch blocks.
- Only use
RuntimeWarningwhen exceptions are meant to be intercepted. - Only use
RuntimeErrorexceptions when something goes wrong that should be fixed, i.e. a bug in the software. - Only catch all exceptions at the beginning of a main function or thread that may not display tracebacks when error happens.
When to use exceptions
try:
# Some code and function calls
except RuntimeError as e:
logging.error(e)
Never do like this!
try:
# Some code and function calls
except RuntimeError
pass
Never do like this!
try:
if something:
raise RuntimeWarning("That went wrong.")
except RuntimeWarning:
pass
finally:
clean_up()
This is not ok.
try:
if something:
raise RuntimeWarning("That went wrong.")
except RuntimeWarning:
do_different()
This is OK.
try:
if something:
raise RuntimeWarning("That went wrong,")
except RuntimeWarning:
raise RuntimeWarning("Explanation what happened.")
This is preferable.
try:
if something:
rasie RuntimeWarning("That went wrong.")
except RuntimeWarning:
pass
else:
raise RuntimeWarning("Explanation what happened.")
This is preferable.
This is how to raise a RuntimeError
import logging
import foo, bar, baz
class SpecialError(RuntimeError):
"""Module specific RuntimeError implementation."""
AINT_WORKING = ("Something is not working.", 100)
SOMETHING_WRONG = ("Something is wrong.", 101)
SUSPICIOUS_ERROR = ("Suspicious error happened.", 102)
def do_stuff():
if not foo.status():
raise SpecialError(*SpecialError.SOMETHING_WRONG, {"meta": foo.info})
if __name__ == "__main__":
try:
do_stuff()
except Exception as e:
logging.critical(e, exc_info=True)
This is how to use RuntimeError based exceptions.
This is how to raise a RuntimeWarning
import foo, bar, baz
class ThisHappened(RuntimeWarning):
pass
class ThatHappened(RuntimeWarning):
pass
def do_this(input):
if what_happened == input:
raise ThisHappened()
foo.is_true()
def do_that(data):
if data:
raise ThatHappened()
try:
do_this(that):
except ThisHappened:
bar.something_else()
This is how to do it.