Skip to content

Try/except (python_try_catch)

What it does: Runs a block of code and redirects execution to the except branch on error.

python_try_catch

Inputs

  • try: main part commands.
  • catch: error handling commands.

Output

  • None (statement block).

When to use it

  • When you want to keep the program running even in the event of an exception.
  • Treatment of risky operations (communication, files, transfers).

Tips and pitfalls

  • In catch always record the error (print/log) so that it can be debugged.
  • Don't hide errors without a response, you would only mask the real problem.

Generated Python example

try:
    data = read_sensor()
except Exception as err_code_str:
    print(str(err_code_str))