<jupyter_start><jupyter_text>ExceptionsPython raises exceptions when it encounters an error.  The idea is that you can trap these exceptions and take an appropriate action instead of causing the code to crash.  The mechanism for this is `try` / `except`.  Here's an example that causes an exception, `ZeroDivisionError`:<jupyter_code>a = 1/0<jupyter_output><empty_output><jupyter_text>and here we handle this<jupyter_code>try:
    a = 1/0
except ZeroDivisionError:
    print("warning: you divided by zero")
    a = 1

a<jupyter_output><empty_output><jupyter_text>another example&mdash;trying to access a key that doesn't exist in a dictionary:<jupyter_code>dict = {"a":1, "b":2, "c":3}
print(dict["d"])<jupyter_output><empty_output><jupyter_text>`KeyError` is the exception that was raised.  We can check for this and take the appropriate action instead<jupyter_code>try:
    val = dict["d"]
except KeyError:
    val = None

print(val)<jupyter_output><empty_output><jupyter_text>To catch and print all exception<jupyter_code>try:
    1/0
except Exception as e:
    print(e)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09882139619220308, 'fraction_numerical': 0.009066183136899365, 'mean_word_length': 5.9, 'pattern_counts': {'":': 3, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '503130', 'license': 'Unlicense', 'n_tokens_mistral': 379, 'n_tokens_neox': 366, 'n_words': 111, 'path': 'day1/06. Python - Exceptions.ipynb', 'repo_name': 'ubutnux/bosscha-python-workshop-2022', 'size': '1103'}