is there a better way of declaring that my functio...
# announcements
h
is there a better way of declaring that my function will throw an exception without using a annotation? Example I use the`Throw` annotation
Copy code
@Throws(NoSuchAlgorithmException::class)
    private fun generateKeyPair(): KeyPair {
        
    }
d
No there is not. Since this feature only exists for Java interoperability there is no direct language feature for it.
h
okay so there is no Kotlin way of doing this at all?
d
No. Since Kotlin does not have checked exceptions there is no need for it. What are you trying to achieve?
h
just trying to convert java code to kotlin
so I can just get a way with using this
Copy code
private fun generateKeyPair(): KeyPair {
        
    }
d
Yes.
👍 1
Unless you want to call the function from Java, in which case you should keep the
Throws
annotation
h
what happens when I call
generateKeyPair
function and it throws an exception, does Kotlin just ignore it?
d
Exceptions in Kotlin behave like RuntimeExceptions in java.
If nobody catches them, they will end up in the thread's UncaughtExceptionHandler
u
this always bothered me, since it hides for example io exceptions when working with files etc.
d
It solves more problems than it creates imho
s
@ursus I had the same question a while back, check this response from Roman
In my case, i opted to have a Type convey the result or error Result<T> or something like that
Callers of my API understand the error possibility while trying to handle the success case
u
@diesieben07 @elizarov while I do agree with you, modeling the error into return type is best, even kotlin std functions for Files do throw
d
Yes, they do throw. But checked exceptions bring all kinds of problems. Kotlin uses lambdas and higher order functions heavily. If you have checked exceptions, you cannot call any "functions that throw" inside those lambdas, because the lambda itself does not throw (unless you catch the exception right there, in the lambda).
That is one reason for unchecked exceptions.
u
right, but then the std api is wrong, i.e. it appears File.readBytes() wont crash, I need atleast a lint or something
d
Define "won't crash".
It won't crash, unless there is an IOException, which is an exceptional state.
u
yes, but expected exeptional state
only unexpected things should crash imo
d
What classifies "expected"? Is "we ran out of memory" unexpected?
u
Depends on the domain. Would you crash the app if your network request failed?
d
No, so I add
catch
blocks.
u
yes, but you dont know to do that unless you look at source
d
If I am reading a file, I know that that can fail.
Example: I am reading from an
InputStream
, can that fail or not? The answer depends on where the data comes from. Is it a
ByteArrayInputStream
? Then it can't fail.
Stuff like this cannot be (reasonably) exposed in the language, this needs to be handled through documentation. Look at any decently sized Java application and you will see stuff like this:
Copy code
try {
   // stuff
} catch (IOException e) {
    throw AssertionError("impossible");
}
u
thats just a coincidence for Files, pick any other custom api
Im not arguing for check exceptions, Im arguing for tooling to not forget handling expected errors, to which you have no knowledge of unless you look at source / doc; since the world is not ready yet to abandon them
d
Okay, but then you have the same problem. How is the IDE supposed to know that this error will be handled further up the call stack?
u
I see your point that @Throws and doc is the same thing which can be out of date or forgot
maybe some static analysis for "throw" keyword?
d
I am not a tooling expert, not sure how feasible that is
u
me neither but Ive forgot to add try catch in kotlin few times already