https://kotlinlang.org logo
Title
k

Kulwinder Singh

07/03/2019, 12:37 PM
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

ribesg

07/03/2019, 12:46 PM
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

karelpeeters

07/03/2019, 12:47 PM
else -> throw e
☝️ 1
k

Kulwinder Singh

07/03/2019, 12:48 PM
is it good to catch
Exception
?
k

karelpeeters

07/03/2019, 12:48 PM
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

streetsofboston

07/03/2019, 12:51 PM
Alas, Kotlin does not (yet) have a multi-exception catch clause, like Java has...
k

Kulwinder Singh

07/03/2019, 12:53 PM
@karelpeeters but because
roundedStrength
throws
all of these exceptions and if i don't
catch
IllegalArgumentException
then how it should be handled
k

karelpeeters

07/03/2019, 12:56 PM
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

Kulwinder Singh

07/03/2019, 12:58 PM
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

karelpeeters

07/03/2019, 12:59 PM
I would recommend against that, it's just going to hide bugs.
r

ribesg

07/03/2019, 1:01 PM
Only catch an exception if you can actually do something about it.
👍 1
k

Kulwinder Singh

07/03/2019, 1:01 PM
so you mean i don't need to handle exception for any of above exceptions OR only
IllegalArgumentException
?
d

diesieben07

07/03/2019, 1:02 PM
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

ribesg

07/03/2019, 1:02 PM
I don’t think you should actually catch any of the exceptions in your code example
d

diesieben07

07/03/2019, 1:03 PM
Yup
Agreed
k

Kulwinder Singh

07/03/2019, 1:03 PM
but app is throwing exceptions to users
r

ribesg

07/03/2019, 1:03 PM
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

diesieben07

07/03/2019, 1:03 PM
then invesitage why these exceptions are happening and fix those bugs
r

ribesg

07/03/2019, 1:03 PM
Then fix the root cause don’t hide it
d

diesieben07

07/03/2019, 1:03 PM
don't just silently ignore them
k

Kulwinder Singh

07/03/2019, 1:05 PM
@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

Vlad

07/03/2019, 1:15 PM
m

mikaelquick

07/03/2019, 4:34 PM
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 !!