1 /* 2 * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 */ 4 5 package kotlinx.coroutines.flow.internal 6 7 import kotlinx.coroutines.* 8 import kotlinx.coroutines.flow.* 9 10 /** 11 * This exception is thrown when operator need no more elements from the flow. 12 * This exception should never escape outside of operator's implementation. 13 * This exception can be safely ignored by non-terminal flow operator if and only if it was caught by its owner 14 * (see usages of [checkOwnership]). 15 */ 16 internal expect class AbortFlowException(owner: FlowCollector<*>) : CancellationException { 17 public val owner: FlowCollector<*> 18 } 19 checkOwnershipnull20internal fun AbortFlowException.checkOwnership(owner: FlowCollector<*>) { 21 if (this.owner !== owner) throw this 22 } 23 24 /** 25 * Exception used to cancel child of [scopedFlow] without cancelling the whole scope. 26 */ 27 internal expect class ChildCancelledException() : CancellationException 28 29 @Suppress("NOTHING_TO_INLINE") 30 @PublishedApi checkIndexOverflownull31internal inline fun checkIndexOverflow(index: Int): Int { 32 if (index < 0) { 33 throw ArithmeticException("Index overflow has happened") 34 } 35 return index 36 } 37