|
| 1 | +"""25_error_handling.md""" |
| 2 | +# pylint: disable=invalid-name |
| 3 | +# pylint: disable=bare-except |
| 4 | +# pylint: disable=unused-variable |
| 5 | +# pylint: disable=import-outside-toplevel |
| 6 | +# pylint: disable=disallowed-name |
| 7 | + |
| 8 | + |
| 9 | +def delta_original(): |
| 10 | + """ |
| 11 | + >>> delta = delta_original() |
| 12 | + >>> delta(1) |
| 13 | + 1.0 |
| 14 | + >>> delta(2) |
| 15 | + 0.5 |
| 16 | + >>> delta(-2) |
| 17 | + 0.5 |
| 18 | + >>> delta(3.14) |
| 19 | + 0.3184713375796178 |
| 20 | + >>> delta(0) |
| 21 | + Traceback (most recent call last): |
| 22 | + ... |
| 23 | + ZeroDivisionError: division by zero |
| 24 | + >>> delta('foo') |
| 25 | + Traceback (most recent call last): |
| 26 | + ... |
| 27 | + TypeError: unsupported operand type(s) for /: 'int' and 'str' |
| 28 | + """ |
| 29 | + # START |
| 30 | + def delta(x): |
| 31 | + return abs(1 / x) |
| 32 | + # END |
| 33 | + return delta |
| 34 | + |
| 35 | + |
| 36 | +def divide_by_zero_guard(): |
| 37 | + """ |
| 38 | + >>> delta = divide_by_zero_guard() |
| 39 | + >>> delta(0) |
| 40 | + >>> delta(2) |
| 41 | + 0.5 |
| 42 | + >>> delta(-2) |
| 43 | + 0.5 |
| 44 | + """ |
| 45 | + # START |
| 46 | + def delta(x): |
| 47 | + if x == 0: |
| 48 | + return None |
| 49 | + return abs(1 / x) |
| 50 | + # END |
| 51 | + return delta |
| 52 | + |
| 53 | + |
| 54 | +def more_guards(): |
| 55 | + """ |
| 56 | + >>> delta = more_guards() |
| 57 | + >>> delta(0) |
| 58 | + >>> delta('foo') |
| 59 | + >>> delta(2) |
| 60 | + 0.5 |
| 61 | + >>> delta(-2) |
| 62 | + 0.5 |
| 63 | + """ |
| 64 | + # START |
| 65 | + def delta(x): |
| 66 | + if x == 0 or not isinstance(x, (float, int)): |
| 67 | + return None |
| 68 | + return abs(1 / x) |
| 69 | + # END |
| 70 | + return delta |
| 71 | + |
| 72 | + |
| 73 | +def bare_except(): |
| 74 | + """ |
| 75 | + >>> bare_except() |
| 76 | + That didn't work, let's carry on and try something else |
| 77 | + """ |
| 78 | + def delta(x): |
| 79 | + return abs(1 / x) |
| 80 | + # START |
| 81 | + try: |
| 82 | + result = delta(0) |
| 83 | + except: |
| 84 | + print("That didn't work, let's carry on and try something else") |
| 85 | + # END |
| 86 | + |
| 87 | + |
| 88 | +def nonbare_excepts(): |
| 89 | + """ |
| 90 | + >>> nonbare_excepts() |
| 91 | + You tried to divide by zero! |
| 92 | + Whoops! |
| 93 | + """ |
| 94 | + def delta(x): |
| 95 | + return abs(1 / x) |
| 96 | + # START |
| 97 | + try: |
| 98 | + result = delta(0) |
| 99 | + except ZeroDivisionError: |
| 100 | + print("You tried to divide by zero!") |
| 101 | + except TypeError: |
| 102 | + print("You tried dividing something that is not divisible!") |
| 103 | + |
| 104 | + # OR |
| 105 | + |
| 106 | + try: |
| 107 | + result = delta(0) |
| 108 | + except (ZeroDivisionError, TypeError): |
| 109 | + print("Whoops!") |
| 110 | + # END |
| 111 | + |
| 112 | + |
| 113 | +def control_flow_else(): |
| 114 | + """ |
| 115 | + >>> control_flow_else() |
| 116 | + Fail |
| 117 | + """ |
| 118 | + def delta(x): |
| 119 | + return abs(1 / x) |
| 120 | + # START |
| 121 | + try: |
| 122 | + n = delta(0) |
| 123 | + except: |
| 124 | + print("Fail") |
| 125 | + else: |
| 126 | + print("Success") |
| 127 | + # END |
| 128 | + |
| 129 | + |
| 130 | +def control_flow_finally(): |
| 131 | + """ |
| 132 | + >>> import random |
| 133 | + >>> random.seed(4) |
| 134 | + >>> control_flow_finally() |
| 135 | + I only print when no error occurs |
| 136 | + I will print every time |
| 137 | + >>> control_flow_finally() |
| 138 | + I only print when an error occurs |
| 139 | + I will print every time |
| 140 | + """ |
| 141 | + import random |
| 142 | + |
| 143 | + def fail_maybe(): |
| 144 | + num = random.randint(0, 1) |
| 145 | + if num == 1: |
| 146 | + raise RuntimeError() |
| 147 | + |
| 148 | + try: |
| 149 | + fail_maybe() |
| 150 | + except RuntimeError: |
| 151 | + print("I only print when an error occurs") |
| 152 | + else: |
| 153 | + print("I only print when no error occurs") |
| 154 | + finally: |
| 155 | + print("I will print every time") |
| 156 | + |
| 157 | + |
| 158 | +# pylint: disable=unnecessary-lambda-assignment |
| 159 | +def overly_broad_is_bad(): |
| 160 | + """ |
| 161 | + >>> overly_broad_is_bad() |
| 162 | + About to do task 4 |
| 163 | + (2, 0) |
| 164 | + """ |
| 165 | + task_1 = task_2 = task_3 = lambda: 0 |
| 166 | + task_4 = task_5 = task_6 = cleanup = lambda: 0 |
| 167 | + |
| 168 | + # START |
| 169 | + try: |
| 170 | + task_1() |
| 171 | + task_2() |
| 172 | + task_3() |
| 173 | + print("About to do task 4") |
| 174 | + task_4() |
| 175 | + foo = 2 + task_5() |
| 176 | + cleanup() |
| 177 | + return foo, task_6() |
| 178 | + except: |
| 179 | + print("Something went wrong!") |
| 180 | + return None |
| 181 | + # END |
| 182 | + |
| 183 | + |
| 184 | +def less_broad(): |
| 185 | + """ |
| 186 | + >>> less_broad() |
| 187 | + About to do task 4 |
| 188 | + (2, 0) |
| 189 | + """ |
| 190 | + task_1 = task_2 = task_3 = lambda: 0 |
| 191 | + task_4 = task_5 = task_6 = cleanup = lambda: 0 |
| 192 | + class TaskError(Exception): |
| 193 | + """pass""" |
| 194 | + |
| 195 | + # START |
| 196 | + task_1() |
| 197 | + task_2() |
| 198 | + |
| 199 | + try: |
| 200 | + task_3() |
| 201 | + except TaskError as e: |
| 202 | + print(f"Task 3 failed! {e}") |
| 203 | + return None |
| 204 | + else: |
| 205 | + print("About to do task 4") |
| 206 | + task_4() |
| 207 | + foo = 2 + task_5() |
| 208 | + return foo, task_6() |
| 209 | + finally: |
| 210 | + cleanup() |
| 211 | + # END |
| 212 | +# pylint: enable=unnecessary-lambda-assignment |
| 213 | + |
| 214 | + |
| 215 | +def raise_your_own_1(): |
| 216 | + """ |
| 217 | + >>> raise_your_own_1() |
| 218 | + """ |
| 219 | + def func(): |
| 220 | + raise RuntimeError("Optional information goes here") |
| 221 | + |
| 222 | + |
| 223 | +def raise_your_own_2(): |
| 224 | + """ |
| 225 | + >>> raise_your_own_2() |
| 226 | + """ |
| 227 | + def func(num): |
| 228 | + try: |
| 229 | + return 2 / num |
| 230 | + except ZeroDivisionError as e: |
| 231 | + raise RuntimeError from e |
| 232 | + |
| 233 | + |
| 234 | +# pylint: disable=missing-class-docstring |
| 235 | +def create_your_own(): |
| 236 | + """ |
| 237 | + >>> create_your_own() |
| 238 | + """ |
| 239 | + class MyException(Exception): |
| 240 | + """Raised when the bad thing happens""" |
| 241 | + |
| 242 | + class MyExceptionChild(MyException): |
| 243 | + pass |
| 244 | +# pylint: enable=missing-class-docstring |
0 commit comments