Exception Handling and Logging#

Exception handling and logging are two important components of programming, in particular for debugging purposes. Detailed technical guides are available online (e.g., Loggin, Error and Exceptions). Here we only repeat a few key points and list a few guidelines for CLIMADA.

Exception handling#

CLIMADA guidelines#

  1. Catch specific exceptions if possible, i.e, if not needed do not catch all exceptions.

  2. Do not catch exception if you do not handle them.

  3. Make a clear explanatory message when you raise an error (similarly to when you use the logger to inform the user). Think of future users and how it helps them understanding the error and debugging their code.

  4. Catch an exception when it arises.

  5. When you catch an exception and raise an error, it is in often (but not always) a good habit to not throw away the first caught exception as it may contain useful information for debugging. (use raise Error from)

#Bad (1)
x = 1
try:
    l = len(events)
    if l < 1:
        print("l is too short")
except:
    pass
#Still bad (2)
try:
    l = len(events)
    if l < 1:
        print("l is too short")
except TypeError:
    pass
#Better, but still unsufficient (3)
try:
    l = len(events)
    if l < 1:
        raise ValueError("To compute an impact there must be at least one event.")
except TypeError:
    raise TypeError("The provided variable events is not a list")
#Even better (4)
try:
    l = len(events)
except TypeError:
    raise TypeError("The provided variable events is not a list")
if l < 1:
    raise ValueError("To compute an impact there must be at least one event.")
#Even better (5)
try:
    l = len(events)
except TypeError as tper:
    raise TypeError("The provided variable events is not a list") from tper
if l < 1:
    raise ValueError("To compute an impact there must be at least one event.")

Exceptions reminder#

Why do we bother to handle exceptions?

  • The most essential benefit is to inform the user of the error, while still allowing the program to proceed.

Logging#

CLIMADA guidelines#

  • In CLIMADA, you cannot use printing. Any output must go into the LOGGER.

  • For any logging messages, always think about the audience. What would a user or developer need for information? This also implies to carefully think about the correct LOGGER level. For instance, some information is for debugging, then use the debug level. In this case, make sure that the message actually helps the debugging process! Some message might just inform the user about certain default parameters, then use the inform level. See below for more details about logger levels.

  • Do not overuse the LOGGER. Think about which level of logging. Logging errors must be useful for debugging.

You can set the level of the LOGGER using climada.util.config.LOGGER.setLevel(logging.XXX). This way you can for instance ‘turn-off’ info messages when you are making an application. For example, setting the logger to the “ERROR” level, use:

import logging
from climada.util.config import LOGGER
LOGGER.setLevel(logging.ERROR)

What levels to use in CLIMADA?

  • Debug: what you would print while developing/debugging

  • Info: information for example in the check instance

  • Warning: whenever CLIMADA fills in values, makes an extrapolation, computes something that might potentially lead to unwanted results (e.g., the 250year damages extrapolated from data over 20 years)

No known use case:

  • Error: instead, raise an Error and add the message (raise ValueError(“Error message”))

  • Critical: …

Reminder about Logging#

“Logging is a means of tracking events that happen when some software runs.”

When to use logging

“Logging provides a set of convenience functions for simple logging usage. These are debug(), info(), warning(), error() and critical(). To determine when to use logging, see the table below, which states, for each of a set of common tasks, the best tool to use for it.”

Doc

Logger level

“The logging functions are named after the level or severity of the events they are used to track. The standard levels and their applicability are described below (in increasing order of severity):”

Doc