Path: blob/master/src/java.base/share/classes/java/util/ConcurrentModificationException.java
41152 views
/*1* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.util;2627/**28* This exception may be thrown by methods that have detected concurrent29* modification of an object when such modification is not permissible.30* <p>31* For example, it is not generally permissible for one thread to modify a Collection32* while another thread is iterating over it. In general, the results of the33* iteration are undefined under these circumstances. Some Iterator34* implementations (including those of all the general purpose collection implementations35* provided by the JRE) may choose to throw this exception if this behavior is36* detected. Iterators that do this are known as <i>fail-fast</i> iterators,37* as they fail quickly and cleanly, rather that risking arbitrary,38* non-deterministic behavior at an undetermined time in the future.39* <p>40* Note that this exception does not always indicate that an object has41* been concurrently modified by a <i>different</i> thread. If a single42* thread issues a sequence of method invocations that violates the43* contract of an object, the object may throw this exception. For44* example, if a thread modifies a collection directly while it is45* iterating over the collection with a fail-fast iterator, the iterator46* will throw this exception.47*48* <p>Note that fail-fast behavior cannot be guaranteed as it is, generally49* speaking, impossible to make any hard guarantees in the presence of50* unsynchronized concurrent modification. Fail-fast operations51* throw {@code ConcurrentModificationException} on a best-effort basis.52* Therefore, it would be wrong to write a program that depended on this53* exception for its correctness: <i>{@code ConcurrentModificationException}54* should be used only to detect bugs.</i>55*56* @author Josh Bloch57* @see Collection58* @see Iterator59* @see Spliterator60* @see ListIterator61* @see Vector62* @see LinkedList63* @see HashSet64* @see Hashtable65* @see TreeMap66* @see AbstractList67* @since 1.268*/69public class ConcurrentModificationException extends RuntimeException {70@java.io.Serial71private static final long serialVersionUID = -3666751008965953603L;7273/**74* Constructs a ConcurrentModificationException with no75* detail message.76*/77public ConcurrentModificationException() {78}7980/**81* Constructs a {@code ConcurrentModificationException} with the82* specified detail message.83*84* @param message the detail message pertaining to this exception.85*/86public ConcurrentModificationException(String message) {87super(message);88}8990/**91* Constructs a new exception with the specified cause and a detail92* message of {@code (cause==null ? null : cause.toString())} (which93* typically contains the class and detail message of {@code cause}.94*95* @param cause the cause (which is saved for later retrieval by the96* {@link Throwable#getCause()} method). (A {@code null} value is97* permitted, and indicates that the cause is nonexistent or98* unknown.)99* @since 1.7100*/101public ConcurrentModificationException(Throwable cause) {102super(cause);103}104105/**106* Constructs a new exception with the specified detail message and107* cause.108*109* <p>Note that the detail message associated with {@code cause} is110* <i>not</i> automatically incorporated in this exception's detail111* message.112*113* @param message the detail message (which is saved for later retrieval114* by the {@link Throwable#getMessage()} method).115* @param cause the cause (which is saved for later retrieval by the116* {@link Throwable#getCause()} method). (A {@code null} value117* is permitted, and indicates that the cause is nonexistent or118* unknown.)119* @since 1.7120*/121public ConcurrentModificationException(String message, Throwable cause) {122super(message, cause);123}124}125126127