is there any better way in kotlin to handle this c...
# announcements
k
is there any better way in kotlin to handle this catch blocks, because in android i am using
Equalizer
class, so in Equalizer class most of the getters/setters
throws
this 4-5
exceptions
. so to access single value i have to add much code
r
Copy code
fun getCurrentBass(): Short = try {
        mBassBoost!!.roundedStrength
    } catch (e: Exception) {
        when (e) {
            is UnsupportedOperationException -> 0
            is IllegalStateException -> 0
            is IllegalArgumentException -> 0
            is java.lang.RuntimeException -> 0
            else -> 42
        }
    }
}
👎 1
👍 1
k
else -> throw e
☝️ 1
k
is it good to catch
Exception
?
k
Well if you rethrow the rest it's fine.
👍 1
This is a strange situation though, are you sure you need to catch all of these exceptions?
For example
IllegalArgumentException
is meant to indicate a code error, so it's never meant to be caught.
And your code doesn't even have arguments!
s
Alas, Kotlin does not (yet) have a multi-exception catch clause, like Java has...
k
@karelpeeters but because
roundedStrength
throws
all of these exceptions and if i don't
catch
IllegalArgumentException
then how it should be handled
k
You don't always need to handle every exception everywhere, sometimes there's just nothing to do about it and you can let it go up the callstack, eventually being logged somewhere or even crashing the program. For example
IllegalArgumentException
just means the program is wrong, there's no point is hiding that and silently returning
0
.
k
ok, but my app is crashing by most of other
exceptions
but also i'm not sure about
IllegalArgumentException
so i have also handled this for safety
k
I would recommend against that, it's just going to hide bugs.
r
Only catch an exception if you can actually do something about it.
👍 1
k
so you mean i don't need to handle exception for any of above exceptions OR only
IllegalArgumentException
?
d
You should only handle an exception if you have a reasonable case where it can happen. Example: A file is not found, so FileNotFoundException woudl be thrown. You could catch that and display an error to the user
r
I don’t think you should actually catch any of the exceptions in your code example
d
Yup
Agreed
k
but app is throwing exceptions to users
r
Just make sure uncaught exception are reported somewhere so that you can be aware of it happening, then fix the root cause. Hiding exceptions like that is the worst thing you can do.
d
then invesitage why these exceptions are happening and fix those bugs
r
Then fix the root cause don’t hide it
d
don't just silently ignore them
k
@diesieben07 @ribesg yes you are right, i also don't like to hide exceptions like this but i have searched a lot about these errors but did not able to find any useful solutions other than
catching
it
v
m
You need to catch thrown exception. But you can rethink the logic. Since you only want to return two different values. if runtimeexception else 0. But think about the logic who shall handle the error? The function that got the error or in a bigger errorhandler. And just a tip remove the !!