From c72316acf12643d5e5dd3e32a84604914eed1853 Mon Sep 17 00:00:00 2001 From: Sad Ellie Date: Fri, 13 Jan 2023 18:58:24 +0400 Subject: [PATCH] Catch divide by zero and sqrt of negative exceptions --- .../unitto/feature/converter/MainViewModel.kt | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/feature/converter/src/main/java/com/sadellie/unitto/feature/converter/MainViewModel.kt b/feature/converter/src/main/java/com/sadellie/unitto/feature/converter/MainViewModel.kt index ceaed4dd..649a387f 100644 --- a/feature/converter/src/main/java/com/sadellie/unitto/feature/converter/MainViewModel.kt +++ b/feature/converter/src/main/java/com/sadellie/unitto/feature/converter/MainViewModel.kt @@ -436,11 +436,16 @@ class MainViewModel @Inject constructor( Expressions().eval(cleanInput) } catch (e: Exception) { when (e) { + // Invalid expression, don't do anything is ArrayIndexOutOfBoundsException, is IndexOutOfBoundsException, - is NumberFormatException, - is ExpressionException, - is ArithmeticException -> return + is ExpressionException -> return + // Divide by zero or SQRT of negative + is NumberFormatException, is ArithmeticException -> { + _calculated.update { null } + _result.update { "" } + return + } else -> throw e } }