[K/N] Allow to unset unhandled exception hook ^KT-49228

This commit is contained in:
Alexander Shabalin
2022-06-28 11:28:01 +03:00
committed by Space
parent 187fe6d50a
commit 41d6dc5914
2 changed files with 16 additions and 0 deletions
@@ -18,4 +18,9 @@ fun main() {
val hook1 = getUnhandledExceptionHook()
assertEquals(exceptionHook, hook1)
val hook2 = getUnhandledExceptionHook()
assertEquals(exceptionHook, hook2)
val hook3 = setUnhandledExceptionHook(null)
assertEquals(exceptionHook, hook3)
val hook4 = getUnhandledExceptionHook()
assertNull(hook4)
}
@@ -57,6 +57,17 @@ public typealias ReportUnhandledExceptionHook = Function1<Throwable, Unit>
* Hook must be a frozen lambda, so that it could be called from any thread/worker.
*/
@OptIn(FreezingIsDeprecated::class)
public fun setUnhandledExceptionHook(hook: ReportUnhandledExceptionHook?): ReportUnhandledExceptionHook? {
try {
return UnhandledExceptionHookHolder.hook.swap(hook)
} catch (e: InvalidMutabilityException) {
throw InvalidMutabilityException("Unhandled exception hook must be frozen")
}
}
@Suppress("CONFLICTING_OVERLOADS")
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
@OptIn(FreezingIsDeprecated::class)
public fun setUnhandledExceptionHook(hook: ReportUnhandledExceptionHook): ReportUnhandledExceptionHook? {
try {
return UnhandledExceptionHookHolder.hook.swap(hook)