| 1 | 1 | def handle_error_by_throwing_exception(): |
| 2 | pass | |
| 2 | raise Exception("An error occurred") | |
| 3 | 3 | |
| 4 | 4 | |
| 5 | 5 | def handle_error_by_returning_none(input_data): |
| 6 | pass | |
| 6 | try: | |
| 7 | return int(input_data) | |
| 8 | except (ValueError, TypeError): | |
| 9 | return None | |
| 7 | 10 | |
| 8 | 11 | |
| 9 | 12 | def handle_error_by_returning_tuple(input_data): |
| 10 | pass | |
| 13 | try: | |
| 14 | return (True, int(input_data)) | |
| 15 | except (ValueError, TypeError): | |
| 16 | return (False, None) | |
| 11 | 17 | |
| 12 | 18 | |
| 13 | 19 | def filelike_objects_are_closed_on_exception(filelike_object): |
| 14 | pass | |
| 20 | with filelike_object: | |
| 21 | filelike_object.do_something() | |
| 22 | ||
| 23 |