KT.EXCEPTION_RAISED_IN_UNEXPECTED_LOCATION

Function should never throw an exception

This rule reports functions which should never throw an exception. If a function exists that does throw an exception it will be reported. By default, this rule checks toString, hashCode, equals and finalize. This rule is configurable via the methodNames configuration to change the list of functions which should not throw any exceptions.

Noncompliant Code

Copy
class Foo {

    override fun toString(): String {
        throw IllegalStateException() // exception should not be thrown here
    }
}

Options

  • methodNames (default: ['equals', 'finalize', 'hashcode', 'tostring'])

    methods which should not throw exceptions

The content on this page is adapted from the Detekt Docs. Copyright ©2022 The Detekt Team. All rights reserved. https://detekt.dev/comments.html