https://kotlinlang.org logo
#getting-started
Title
# getting-started
y

y

10/12/2023, 4:49 PM
is it okay to throw exceptions in a
synchronized
block?
e

ephemient

10/12/2023, 4:54 PM
if it makes sense to
y

y

10/12/2023, 4:55 PM
as in, it's safe: no hidden gotchas to be aware of
e

ephemient

10/12/2023, 4:55 PM
it's basically
Copy code
try {
    MONITORENTER
    block()
} finally {
    MONITOREXIT
}
(you can't write those bytecode instructions in pure Kotlin but the compiler can)
y

y

10/12/2023, 4:57 PM
oh. yeah that's as clear as I can hope for, as a person who knows basically nothing about the jvm.
j

Joffrey

10/13/2023, 8:13 AM
In such constructs, the
finally
block always executes, whether the
try
block completed normally or threw an exception. You can read about
try
-
catch
-
finally
constructs in this section of the Java documentation: https://docs.oracle.com/javase/tutorial/essential/exceptions/handling.html And more specifically about `finally`: https://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html
3 Views