user155
user155

Reputation: 805

Weird crashes "java.lang.ArithmeticException divide by zero"

Recently I found out from Firebase Crashlytics that some HTC/Fortuneship devices crashes with the next exception and I can't understand why

enter image description here

val formattedViews: String
   get() = String.format(Locale.getDefault(), "%,d %s", viewCount, Util.getString(R.string.views))

I don't get how such exception can happen in this code

Is there something wrong with those devices?

enter image description here

Upvotes: 3

Views: 799

Answers (1)

Ben P.
Ben P.

Reputation: 54204

This is a bug in the JDK shipping with Android 7: https://bugs.openjdk.java.net/browse/JDK-8167567

You are passing Locale.getDefault() to format(), which is the same as not specifying a locale at all. The only known workaround appears to be to use a known-good locale to do the formatting (e.g. Locale.US), but of course this means you won't get locale-specific thousands groupings for users outside the US.

Or you could change your format specification to not use groupings (i.e. %d without the comma).

Perhaps you could catch the exception and fall back to US formatting for users who would otherwise crash? That's what these guys did: https://github.com/wordpress-mobile/WordPress-Android/pull/5604/files

Upvotes: 3

Related Questions