blob: 462d816d789c2615cd4acc7ae532806dc572b9a8 [file] [log] [blame]
Roman Elizarovf29203c2018-01-11 12:39:36 +03001/*
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
17package kotlinx.coroutines.experimental
18
Roman Elizarovf29203c2018-01-11 12:39:36 +030019/**
20 * This exception gets thrown if an exception is caught while processing [CompletionHandler] invocation for [Job].
21 */
22public actual class CompletionHandlerException actual constructor(
23 message: String,
24 cause: Throwable
25) : RuntimeException(message, cause)
26
Roman Elizarovaa461cf2018-04-11 13:20:29 +030027/**
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 Elizarovf29203c2018-01-11 12:39:36 +030033public 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 */
40public actual class JobCancellationException public actual constructor(
41 message: String,
42 cause: Throwable?,
Roman Elizarove89cd682018-04-25 13:03:40 +030043 @JvmField internal actual val job: Job
Roman Elizarovf29203c2018-01-11 12:39:36 +030044) : CancellationException(message) {
Vsevolod Tolstopyatovf5e63ca2018-04-12 19:59:56 +030045
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 Elizarovf29203c2018-01-11 12:39:36 +030063 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 Elizarovf29203c2018-01-11 12:39:36 +030071internal actual class DispatchException actual constructor(message: String, cause: Throwable) : RuntimeException(message, cause)
Roman Elizarovaa461cf2018-04-11 13:20:29 +030072
73@Suppress("NOTHING_TO_INLINE")
74internal actual inline fun Throwable.addSuppressedThrowable(other: Throwable) =
75 addSuppressed(other)