https://kotlinlang.org logo
Title
c

changd

08/21/2019, 9:26 PM
i'm having some java interop issues with
@Throws
The API
interface StormcrowBase {
   @get:Throws(Exception::class)
   val featureNames: ArrayList<String>
}
Java call site
try {
                features = stormcrowBase.getFeatureNames();
            } catch (Exception e) {
                e.printStackTrace();
            }
The error I'm getting is
Exception is never thrown in the corresponding block
. Any ideas?
l

LeoColman

08/21/2019, 10:01 PM
What happens if you don't use
get:@Throws
and use
@Throws
instead?
s

Shawn

08/21/2019, 11:18 PM
that’s a compile error
c

changd

08/21/2019, 11:53 PM
yea, i haven't been able to resolve this. I switched from
val
to
fun
and the Java interop is working.. but the Kotlin API is pretty ugly now.
@Throws(Exception::class)
   fun getFeatureNames(): ArrayList<String>
Funny enough, the java generated decompiled class for both
val
and
fun
look identical
l

LeoColman

08/21/2019, 11:54 PM
Throwing RuntimeExceptions is the rule for Java in a long long time
Why do you need @Throws anyway?
c

changd

08/21/2019, 11:54 PM
i obfuscated some of the code ^^, this is to interop with our existing Java codebase
Throwing RuntimeExceptions is the rule for Java in a long long time
i've never heard that
l

LeoColman

08/21/2019, 11:55 PM
I understand, but does your Java code base requires you to catch exception always?
i've never heard that
Clean code establishes that throwing checked exception is bad, and throwing RuntimeException is better, as you don't need to catch them everywhere, unless required
c

changd

08/21/2019, 11:56 PM
this is a pretty large refactor, changing ~100 call sites
there are different use cases
i suppose i could just write this interface in Java instead
l

LeoColman

08/21/2019, 11:57 PM
But you can still catch an exception, even if it's Unchecked, right?
Perhaps refactoring isn't very good indeed
For this case
But consider throwing unchecked exceptions in the future
c

changd

08/21/2019, 11:58 PM
we're writing all new code in Kotlin, and also this is for Android.. i think generally in Java-Android world, we prefer checked exceptions
l

LeoColman

08/21/2019, 11:59 PM
As an Android dev myself, my team have always prefered to use unchecked exceptions. The same clean code practices should be applied
But this discussion is not the point here tho
I agree, maybe this interface can be written in Java
c

changd

08/22/2019, 12:00 AM
thanks for the suggestions
now if only there's Kotlin -> Java conversion 😄
l

LeoColman

08/22/2019, 12:02 AM
I think there is tho
Try searching with CTRL SHIFT A