Skip to content

Files

Latest commit

 

History

History
38 lines (27 loc) · 883 Bytes

IteratorNotThrowingNoSuchElementException.md

File metadata and controls

38 lines (27 loc) · 883 Bytes

Pattern: Missing NoSuchElementException for Iterator

Issue: -

Description

Reports implementations of the Iterator interface which do not throw a NoSuchElementException in the implementation of the next() method. When there are no more elements to return an Iterator should throw a NoSuchElementException.

Example of incorrect code:

class MyIterator : Iterator<String> {

    override fun next(): String {
        return ""
    }
}

Example of correct code:

class MyIterator : Iterator<String> {

    override fun next(): String {
        if (!this.hasNext()) {
            throw NoSuchElementException()
        }
        // ...
    }
}

Further Reading