Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-2017 JetBrains s.r.o. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package kotlinx.coroutines.experimental |
| 18 | |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 19 | /** |
| 20 | * This exception gets thrown if an exception is caught while processing [CompletionHandler] invocation for [Job]. |
| 21 | */ |
| 22 | public actual class CompletionHandlerException actual constructor( |
| 23 | message: String, |
| 24 | cause: Throwable |
| 25 | ) : RuntimeException(message, cause) |
| 26 | |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 27 | /** |
| 28 | * Thrown by cancellable suspending functions if the [Job] of the coroutine is cancelled while it is suspending. |
| 29 | * It indicates _normal_ cancellation of a coroutine. |
| 30 | * **It is not printed to console/log by default uncaught exception handler**. |
| 31 | * (see [handleCoroutineException]). |
| 32 | */ |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 33 | public actual typealias CancellationException = java.util.concurrent.CancellationException |
| 34 | |
| 35 | /** |
| 36 | * Thrown by cancellable suspending functions if the [Job] of the coroutine is cancelled or completed |
| 37 | * without cause, or with a cause or exception that is not [CancellationException] |
| 38 | * (see [Job.getCancellationException]). |
| 39 | */ |
| 40 | public actual class JobCancellationException public actual constructor( |
| 41 | message: String, |
| 42 | cause: Throwable?, |
Roman Elizarov | e89cd68 | 2018-04-25 13:03:40 +0300 | [diff] [blame] | 43 | @JvmField internal actual val job: Job |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 44 | ) : CancellationException(message) { |
Vsevolod Tolstopyatov | f5e63ca | 2018-04-12 19:59:56 +0300 | [diff] [blame] | 45 | |
| 46 | init { |
| 47 | if (cause != null) initCause(cause) |
| 48 | } |
| 49 | |
| 50 | override fun fillInStackTrace(): Throwable { |
| 51 | if (DEBUG) { |
| 52 | return super.fillInStackTrace() |
| 53 | } |
| 54 | |
| 55 | /* |
| 56 | * In non-debug mode we don't want to have a stacktrace on every cancellation/close, |
| 57 | * parent job reference is enough. Stacktrace of JCE is not needed most of the time (e.g., it is not logged) |
| 58 | * and hurts performance. |
| 59 | */ |
| 60 | return this |
| 61 | } |
| 62 | |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 63 | override fun toString(): String = "${super.toString()}; job=$job" |
| 64 | override fun equals(other: Any?): Boolean = |
| 65 | other === this || |
| 66 | other is JobCancellationException && other.message == message && other.job == job && other.cause == cause |
| 67 | override fun hashCode(): Int = |
| 68 | (message!!.hashCode() * 31 + job.hashCode()) * 31 + (cause?.hashCode() ?: 0) |
| 69 | } |
| 70 | |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 71 | internal actual class DispatchException actual constructor(message: String, cause: Throwable) : RuntimeException(message, cause) |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 72 | |
| 73 | @Suppress("NOTHING_TO_INLINE") |
| 74 | internal actual inline fun Throwable.addSuppressedThrowable(other: Throwable) = |
| 75 | addSuppressed(other) |