|
| 1 | +# Exceptions |
| 2 | + |
| 3 | +> [!NOTE] |
| 4 | +> Exceptions from Python's `threading` module are not included |
| 5 | +
|
| 6 | +<br /> |
| 7 | +<details> |
| 8 | + <summary>Jump to</summary> |
| 9 | + <ul> |
| 10 | + <li><a href='#ignoring-exceptions'> Ignoring Exceptions</a></li> |
| 11 | + <li><a href='#exceptions-1'> Exceptions </a></li> |
| 12 | + </ul> |
| 13 | +</details> |
| 14 | + |
| 15 | + |
| 16 | +Don't have the thread library? [See here](./getting-started.md) for installing thread |
| 17 | + |
| 18 | +--- |
| 19 | + |
| 20 | +## Ignoring exceptions |
| 21 | + |
| 22 | +When initializing a thread, you can parse a [**suppress_errors**](./threading.md#parameters) bool.<br /> |
| 23 | +By default it is false, but if set to true, exceptions will not be propagated but just stored within `Thread._errors` |
| 24 | + |
| 25 | +When initializing a thread, you can parse a [**ignore_errors**](./threading.md#parameters) sequence.<br /> |
| 26 | +By default it is an empty tuple.<br /> |
| 27 | +Ignored errors will not be propagated and not stored within `Thread._errors` |
| 28 | + |
| 29 | +<br /> |
| 30 | + |
| 31 | +### Example |
| 32 | +```py |
| 33 | +from thread import Thread |
| 34 | + |
| 35 | +def bad_function(): |
| 36 | + raise RuntimeError('>:cc') |
| 37 | + |
| 38 | + |
| 39 | +# Normal behaviour |
| 40 | +thread0 = Thread( |
| 41 | + target = bad_function |
| 42 | +) |
| 43 | +thread0.start() |
| 44 | +thread0.join() |
| 45 | +# exit(1) RuntimeError(':<<') |
| 46 | + |
| 47 | + |
| 48 | +# Suppress exceptions |
| 49 | +thread1 = Thread( |
| 50 | + target = bad_function, |
| 51 | + suppress_errors = True |
| 52 | +) |
| 53 | +thread1.start() |
| 54 | +thread1.join() |
| 55 | +print(thread1._errors) # list[RuntimeError('>:cc')] |
| 56 | + |
| 57 | + |
| 58 | +# Ignore error |
| 59 | +thread2 = Thread( |
| 60 | + target = bad_function, |
| 61 | + ignore_errors = [RuntimeError] |
| 62 | +) |
| 63 | +thread2.start() |
| 64 | +thread2.join() |
| 65 | +print(thread2._errors) # list[] |
| 66 | + |
| 67 | + |
| 68 | +# Non-ignored error |
| 69 | +thread3 = Thread( |
| 70 | + target = bad_function, |
| 71 | + ignore_errors = [ValueError] |
| 72 | +) |
| 73 | +thread3.start() |
| 74 | +thread3.join() |
| 75 | +# exit(1) RuntimeError(':<<') |
| 76 | + |
| 77 | + |
| 78 | +# Non-ignored error with suppressing |
| 79 | +thread4 = Thread( |
| 80 | + target = bad_function, |
| 81 | + ignore_errors = [ValueError], |
| 82 | + suppress_errors = True |
| 83 | +) |
| 84 | +thread4.start() |
| 85 | +thread4.join() |
| 86 | +print(thread4._errors) # list[RuntimeError(':<<')] |
| 87 | + |
| 88 | + |
| 89 | +# Ignored error with suppressing |
| 90 | +thread5 = Thread( |
| 91 | + target = bad_function, |
| 92 | + ignore_errors = [RuntimeError], |
| 93 | + suppress_errors = True |
| 94 | +) |
| 95 | +thread5.start() |
| 96 | +thread5.join() |
| 97 | +print(thread5._errors) # list[] |
| 98 | +``` |
| 99 | + |
| 100 | +<br /> |
| 101 | + |
| 102 | + |
| 103 | +## Exceptions |
| 104 | + |
| 105 | +The list of exceptions that can be thrown |
| 106 | + |
| 107 | +<br /> |
| 108 | + |
| 109 | + |
| 110 | +### ThreadErrorBase |
| 111 | + |
| 112 | +This is the base exception class that all exceptions inherit from |
| 113 | + |
| 114 | +<br /> |
| 115 | + |
| 116 | + |
| 117 | +### ThreadStillRunningError |
| 118 | + |
| 119 | +This is raised when you attempt to invoke a method which requries the thread to not be running, but is running. |
| 120 | +> You can wait for the thread to terminate with [**Thread.join()**](./threading.md#methods) before invoking the method |
| 121 | +
|
| 122 | +> You can check if the thread is running with [**Thread.is_alive()**](threading.md#methods) before invoking the method |
| 123 | +
|
| 124 | +<br /> |
| 125 | + |
| 126 | + |
| 127 | +### ThreadNotRunningError |
| 128 | + |
| 129 | +This is raised when you attempt to invoke a method which requires the thread to be running, but isn't |
| 130 | +> You can run the thread with [**Thread.start()**](threading.md#methods) before invoking the method |
| 131 | +
|
| 132 | +<br /> |
| 133 | + |
| 134 | + |
| 135 | +### ThreadNotInitializedError |
| 136 | + |
| 137 | +This is raised when you attempt to invoke a method which requires the thread to be initialized, but isn't |
| 138 | +> You can initialize and start the thread with [**Thread.start()**](threading.md#methods) before invoking the method |
| 139 | +
|
| 140 | +<br /> |
| 141 | + |
| 142 | + |
| 143 | +### HookRuntimeError |
| 144 | + |
| 145 | +This is raised when hooks raise an exception<br /> |
| 146 | +Conforms to when thread is ran with errors suppressed or ignored |
| 147 | + |
| 148 | +Example traceback |
| 149 | +```text |
| 150 | +HookRuntimeError: Encountered runtime errors in hooks |
| 151 | +
|
| 152 | +1. my_function |
| 153 | +>>>>>>>>>> |
| 154 | +/usr/home/proj/main.py:50 |
| 155 | +ZeroDivisionError: |
| 156 | +<<<<<<<<<< |
| 157 | +
|
| 158 | +2. my_otherfunction |
| 159 | +>>>>>>>>>> |
| 160 | +ImportError: |
| 161 | +<<<<<<<<<< |
| 162 | +``` |
| 163 | + |
| 164 | +<br /> |
| 165 | + |
| 166 | +[See here](./threading.md) for how to using the `thread.Thread` class! |
0 commit comments