I wonder why compiler not complaining for try/catc...
# announcements
n
I wonder why compiler not complaining for try/catch when we use
Copy code
first()
with collections, It might throw an Exception
z
Kotlin does not have checked exceptions
✔️ 2
n
yea that sucks
1
so we need to be careful by our own
c
This article in the docs briefly explains their rationale for why there are no checked exceptions https://kotlinlang.org/docs/reference/exceptions.html
Basically, by having no checked exceptions, thrown exceptions are bubbled to higher levels of the application that are better-suited to properly handling them, rather than forcing deeper levels to worry about it and become overly verbose, or catching and silently ignoring them
n
totally agree, but it would be great if they have compile time warnings related to it . So at least developers know that there is a bit of risk and in case of Android dsevelopment this might result s to app crash
z
Even with checked exceptions, as long as unchecked exceptions exist (i.e. even in Java) any method call can throw an exception and potentially crash your app.
👆 2
c
You can use the
@Throws
annotation for documentation purposes, which does get translated to the
throws
clause when used from Java https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html#checked-exceptions. Though the better solution is still just to provide more centralized error-handling and your own APIs/extensions that ensure they are applied in all situations
h
There is a method firstOrNull() for your usecase. It doesn't throw but returns null If no first element, forcing you to handle that case, and that all without exceptions ✌️
👍 1
n
yea I am using
Copy code
firstOrNull
my only concern if we get any warning from compiler side