nadi
03/03/2024, 2:11 PMJoffrey
03/03/2024, 2:14 PMException
and use a when
in the catch block. But honestly if you need to do something more than a one liner I'd suggest extracting a function anyway.nadi
03/03/2024, 2:15 PMnadi
03/03/2024, 2:15 PMJoffrey
03/03/2024, 2:21 PMwhen
approach will lead to more nesting and boilerplate than a simple function and 2 different catch blocks. Because if you have a when
you also should have an else
that rethrows everything you don't handle:
See:
try {
// Main code
} catch(e: Exception) {
when(e) {
is Exception1,
is Exception2 -> {
// Complex
// Error
// Handling
}
else -> throw e
}
}
VS
try {
// Main code
} catch(e: Exception1) {
handleError(e)
} catch(e: Exception2) {
handleError(e)
}
private fun handleError(e: Exception) {
// Complex
// Error
// Handling
}
ephemient
03/03/2024, 2:25 PMephemient
03/03/2024, 2:29 PMJoffrey
03/03/2024, 2:33 PMephemient
03/03/2024, 2:36 PMnadi
03/03/2024, 2:38 PMStephan Schröder
03/05/2024, 8:38 AMJoffrey
03/05/2024, 8:41 AMStephan Schröder
03/05/2024, 8:45 AMephemient
03/06/2024, 11:15 PMrunCatching
should really be annotated @DelicateApi
or something like that. it makes doing the wrong thing far too easyStephan Schröder
03/07/2024, 10:38 AMJoffrey
03/07/2024, 10:58 AMError
class, "reasonable applications shouldn't try to catch them"