Pattern: Lost exception
Issue: -
Exceptions should not be swallowed. This rule reports all instances where exceptions are caught
and not correctly passed into a newly thrown exception.
Example of incorrect code:
fun foo() {
try {
// ...
} catch(e: IOException) {
throw MyException(e.message) // e is swallowed
}
try {
// ...
} catch(e: IOException) {
throw MyException() // e is swallowed
}
try {
// ...
} catch(e: IOException) {
bar() // exception is unused
}
}
Example of correct code:
fun foo() {
try {
// ...
} catch(e: IOException) {
throw MyException(e)
}
try {
// ...
} catch(e: IOException) {
println(e) // logging is ok here
}
}