Skip to content

Files

Latest commit

 

History

History
35 lines (26 loc) · 751 Bytes

ThrowingNewInstanceOfSameException.md

File metadata and controls

35 lines (26 loc) · 751 Bytes

Pattern: Throwing new instance of same exception

Issue: -

Description

Exceptions should not be wrapped inside the same exception type and then rethrown. Prefer wrapping exceptions in more meaningful exception types.

Example of incorrect code:

fun foo() {
    try {
        // ...
    } catch (e: IllegalStateException) {
        throw IllegalStateException(e) // rethrows the same exception
    }
}

Example of correct code:

fun foo() {
    try {
        // ...
    } catch (e: IllegalStateException) {
        throw MyException(e)
    }
}

Further Reading