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

João Gabriel Zó

02/24/2023, 5:19 PM
does
try-catch
actually throw the exception before catching it?
j

Joffrey

02/24/2023, 5:19 PM
The code inside the
try
block throws an exception, and then the
catch
block is executed with the thrown exception
s

Stephan Schröder

03/06/2023, 9:27 AM
well, at least the code in the
try
block is supposed to be capable of throwing the particular exception that the
catch
block takes care of, but since unchecked exceptions doesn't have to be declared (and all exceptions in Kotlin are unchecked), I can be hard to figure out where a particular exception can be thrown. And it's also possible that after a refactoring or code change, a particular exception isn't thrown any more, but due to an overside of the dev who did the code change, the catch-block wasn't updated as well. The compiler won't complain about unneccessary catch-blocks, but IDEs should mark unused exceptions (at least IntelliJ does).