✨ Add exception suppressor that run code when an exception is caught
[rhynodge.git] / src / main / kotlin / net / pterodactylus / util / exception / Exceptions.kt
1 package net.pterodactylus.util.exception
2
3 /**
4  * Runs the given [runnable][Runnable], catching any [exceptions][Exception],
5  * and running the [errorRunnable][Runnable] with a caught exception.
6  *
7  * @param runnable The block to run
8  * @param errorRunnable The block to run in case of an exception
9  */
10 fun suppressException(runnable: () -> Unit = {}, errorRunnable: (e: Exception) -> Unit = {}): () -> Unit = {
11         try {
12                 runnable()
13         } catch (e: Exception) {
14                 errorRunnable(e)
15         }
16 }