NoSuchElementException in Java
The NoSuchElementException is thrown by an Enumeration's nextElement method which indicates that there are no more elements left in the enumeration. The NoSuchElementException is thrown by the following methods-
The NoSuchElementException is a subclass of RuntimeException and implements the Serializable interface.
Constructors of NoSuchElementException
Example of NoSuchElementException
How to avoid NoSuchElementException?
One of the most common scenarios where the NoSuchElementException occurs is when we are iterating over an empty Set. If we wish to avoid this exception, we can put a check before iterating over the set. While iterating, check every time that there is an element present in the set afterward as follows-
This way ensures that any element is being accessed if it exists.
Next TopicJava Tutorial
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week