Skip to content

Latest commit

 

History

History
50 lines (41 loc) · 988 Bytes

SwallowedException.md

File metadata and controls

50 lines (41 loc) · 988 Bytes

Pattern: Lost exception

Issue: -

Description

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
    }
}

Further Reading