i'm having some java interop issues with `@Throws`...
# announcements
c
i'm having some java interop issues with
@Throws
The API
Copy code
interface StormcrowBase {
   @get:Throws(Exception::class)
   val featureNames: ArrayList<String>
}
Java call site
Copy code
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
What happens if you don't use
get:@Throws
and use
@Throws
instead?
s
that’s a compile error
c
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.
Copy code
@Throws(Exception::class)
   fun getFeatureNames(): ArrayList<String>
Funny enough, the java generated decompiled class for both
val
and
fun
look identical
l
Throwing RuntimeExceptions is the rule for Java in a long long time
Why do you need @Throws anyway?
c
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
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
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
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
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
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
thanks for the suggestions
now if only there's Kotlin -> Java conversion đŸ˜„
l
I think there is tho
Try searching with CTRL SHIFT A