diff --git a/js/js.libraries/src/core/exceptions.kt b/js/js.libraries/src/core/exceptions.kt index e0c2865f9f5..c0dead984ff 100644 --- a/js/js.libraries/src/core/exceptions.kt +++ b/js/js.libraries/src/core/exceptions.kt @@ -16,32 +16,96 @@ package kotlin -public open class Error(message: String? = null) : Throwable(message, null) +public open class Error : Throwable { + constructor() : super() + constructor(message: String?) : super(message) + constructor(message: String?, cause: Throwable?): super(message, cause) + constructor(cause: Throwable?) : super(cause) +} -public open class Exception(message: String? = null) : Throwable(message, null) +public open class Exception : Throwable { + constructor() : super() + constructor(message: String?) : super(message) + constructor(message: String?, cause: Throwable?): super(message, cause) + constructor(cause: Throwable?) : super(cause) +} -public open class RuntimeException(message: String? = null) : Exception(message) +public open class RuntimeException : Exception { + constructor() : super() + constructor(message: String?) : super(message) + constructor(message: String?, cause: Throwable?): super(message, cause) + constructor(cause: Throwable?) : super(cause) +} -public open class IllegalArgumentException(message: String? = null) : RuntimeException(message) +public open class IllegalArgumentException : RuntimeException { + constructor() : super() + constructor(message: String?) : super(message) + constructor(message: String?, cause: Throwable?): super(message, cause) + constructor(cause: Throwable?) : super(cause) +} -public open class IllegalStateException(message: String? = null) : RuntimeException(message) +public open class IllegalStateException : RuntimeException { + constructor() : super() + constructor(message: String?) : super(message) + constructor(message: String?, cause: Throwable?): super(message, cause) + constructor(cause: Throwable?) : super(cause) +} -public open class IndexOutOfBoundsException(message: String? = null) : RuntimeException(message) +public open class IndexOutOfBoundsException : RuntimeException { + constructor() : super() + constructor(message: String?) : super(message) +} -public open class ConcurrentModificationException(message: String? = null) : RuntimeException(message) +public open class ConcurrentModificationException : RuntimeException { + constructor() : super() + constructor(message: String?) : super(message) + constructor(message: String?, cause: Throwable?): super(message, cause) + constructor(cause: Throwable?) : super(cause) +} -public open class UnsupportedOperationException(message: String? = null) : RuntimeException(message) +public open class UnsupportedOperationException : RuntimeException { + constructor() : super() + constructor(message: String?) : super(message) + constructor(message: String?, cause: Throwable?): super(message, cause) + constructor(cause: Throwable?) : super(cause) +} -public open class NumberFormatException(message: String? = null) : RuntimeException(message) +public open class NumberFormatException : IllegalArgumentException { + constructor() : super() + constructor(message: String?) : super(message) +} -public open class NullPointerException(message: String? = null) : RuntimeException(message) +public open class NullPointerException : RuntimeException { + constructor() : super() + constructor(message: String?) : super(message) +} -public open class ClassCastException(message: String? = null) : RuntimeException(message) +public open class ClassCastException : RuntimeException { + constructor() : super() + constructor(message: String?) : super(message) +} -public open class AssertionError(message: String? = null) : Error(message) +public open class AssertionError : Error { + constructor() : super() + constructor(message: String?) : super(message) + constructor(message: Any?) : super(message.toString(), message as? Throwable) +} -public open class NoSuchElementException(message: String? = null) : Exception(message) +public open class NoSuchElementException : Exception { + constructor() : super() + constructor(message: String?) : super(message) +} -public open class NoWhenBranchMatchedException(message: String? = null) : RuntimeException(message) +public open class NoWhenBranchMatchedException : RuntimeException { + constructor() : super() + constructor(message: String?) : super(message) + constructor(message: String?, cause: Throwable?): super(message, cause) + constructor(cause: Throwable?) : super(cause) +} -public open class UninitializedPropertyAccessException(message: String? = null) : RuntimeException(message) +public open class UninitializedPropertyAccessException : RuntimeException { + constructor() : super() + constructor(message: String?) : super(message) + constructor(message: String?, cause: Throwable?): super(message, cause) + constructor(cause: Throwable?) : super(cause) +} diff --git a/libraries/stdlib/common/src/kotlin/ExceptionsH.kt b/libraries/stdlib/common/src/kotlin/ExceptionsH.kt new file mode 100644 index 00000000000..fbcc0ea4fb7 --- /dev/null +++ b/libraries/stdlib/common/src/kotlin/ExceptionsH.kt @@ -0,0 +1,95 @@ +package kotlin + + +public expect open class Error : Throwable { + constructor() + constructor(message: String?) + constructor(message: String?, cause: Throwable?) + constructor(cause: Throwable?) +} + +public expect open class Exception : Throwable { + constructor() + constructor(message: String?) + constructor(message: String?, cause: Throwable?) + constructor(cause: Throwable?) +} + +public expect open class RuntimeException : Exception { + constructor() + constructor(message: String?) + constructor(message: String?, cause: Throwable?) + constructor(cause: Throwable?) +} + +public expect open class IllegalArgumentException : RuntimeException { + constructor() + constructor(message: String?) + constructor(message: String?, cause: Throwable?) + constructor(cause: Throwable?) +} + +public expect open class IllegalStateException : RuntimeException { + constructor() + constructor(message: String?) + constructor(message: String?, cause: Throwable?) + constructor(cause: Throwable?) +} + +public expect open class IndexOutOfBoundsException : RuntimeException { + constructor() + constructor(message: String?) +} + +public expect open class ConcurrentModificationException : RuntimeException { + constructor() + constructor(message: String?) + constructor(message: String?, cause: Throwable?) + constructor(cause: Throwable?) +} + +public expect open class UnsupportedOperationException : RuntimeException { + constructor() + constructor(message: String?) + constructor(message: String?, cause: Throwable?) + constructor(cause: Throwable?) +} + +public expect open class NumberFormatException : IllegalArgumentException { + constructor() + constructor(message: String?) +} + +public expect open class NullPointerException : RuntimeException { + constructor() + constructor(message: String?) +} + +public expect open class ClassCastException : RuntimeException { + constructor() + constructor(message: String?) +} + +public expect open class AssertionError : Error { + constructor() + constructor(message: Any?) +} + +public expect open class NoSuchElementException : Exception { + constructor() + constructor(message: String?) +} + +public expect open class NoWhenBranchMatchedException : RuntimeException { + constructor() + constructor(message: String?) + constructor(message: String?, cause: Throwable?) + constructor(cause: Throwable?) +} + +public expect open class UninitializedPropertyAccessException : RuntimeException { + constructor() + constructor(message: String?) + constructor(message: String?, cause: Throwable?) + constructor(cause: Throwable?) +} diff --git a/libraries/stdlib/common/src/kotlin/KotlinH.kt b/libraries/stdlib/common/src/kotlin/KotlinH.kt index c0cfd5d9eed..06956560de3 100644 --- a/libraries/stdlib/common/src/kotlin/KotlinH.kt +++ b/libraries/stdlib/common/src/kotlin/KotlinH.kt @@ -19,51 +19,6 @@ package kotlin import kotlin.annotation.AnnotationTarget.FIELD import kotlin.annotation.AnnotationTarget.PROPERTY -open expect class Error : Throwable { - constructor() - constructor(message: String) -} - -open expect class Exception : Throwable { - constructor() - constructor(message: String) -} - -open expect class IllegalArgumentException : RuntimeException { - constructor() - constructor(message: String) -} - -open expect class IllegalStateException : RuntimeException { - constructor() - constructor(message: String) -} - -open expect class IndexOutOfBoundsException : RuntimeException { - constructor() - constructor(message: String) -} - -open expect class NoSuchElementException : RuntimeException { - constructor() - constructor(message: String) -} - -open expect class RuntimeException : Exception { - constructor() - constructor(message: String) -} - -open expect class UnsupportedOperationException : RuntimeException { - constructor() - constructor(message: String) -} - -// TODO: Provide typealias impl in stdlib-jvm -open expect class AssertionError : Error { - constructor() - constructor(message: String) -} expect interface Comparator { diff --git a/libraries/stdlib/test/ExceptionTest.kt b/libraries/stdlib/test/ExceptionTest.kt new file mode 100644 index 00000000000..87dd379aed9 --- /dev/null +++ b/libraries/stdlib/test/ExceptionTest.kt @@ -0,0 +1,76 @@ +/* + * Copyright 2010-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package test.exceptions + +import kotlin.test.* + +class ExceptionTest { + private val cause = Exception("cause") + + @Test fun throwable() = testCreateException(::Throwable, ::Throwable, ::Throwable, ::Throwable) + @Test fun error() = testCreateException(::Error, ::Error, ::Error, ::Error) + @Test fun exception() = testCreateException(::Exception, ::Exception, ::Exception, ::Exception) + @Test fun runtimeException() = testCreateException(::RuntimeException, ::RuntimeException, ::RuntimeException, ::RuntimeException) + @Test fun illegalArgumentException() = testCreateException(::IllegalArgumentException, ::IllegalArgumentException, ::IllegalArgumentException, ::IllegalArgumentException) + @Test fun illegalStateException() = testCreateException(::IllegalStateException, ::IllegalStateException, ::IllegalStateException, ::IllegalStateException) + @Test fun indexOutOfBoundsException() = testCreateException(::IndexOutOfBoundsException, ::IndexOutOfBoundsException) + @Test fun unsupportedOperationException() = testCreateException(::UnsupportedOperationException, ::UnsupportedOperationException, ::UnsupportedOperationException, ::UnsupportedOperationException) + @Test fun numberFormatException() = testCreateException(::NumberFormatException, ::NumberFormatException) + @Test fun nullPointerException() = testCreateException(::NullPointerException, ::NullPointerException) + @Test fun classCastException() = testCreateException(::ClassCastException, ::ClassCastException) + @Test fun noSuchElementException() = testCreateException(::NoSuchElementException, ::NoSuchElementException) + + @Test fun noWhenBranchMatchedException() = testCreateException(::NoWhenBranchMatchedException, ::NoWhenBranchMatchedException, ::NoWhenBranchMatchedException, ::NoWhenBranchMatchedException) + + // TODO: Sort out with nullability of the parameters + // @Test fun uninitializedPropertyAccessException() = testCreateException(::UninitializedPropertyAccessException, ::UninitializedPropertyAccessException, ::UninitializedPropertyAccessException, ::UninitializedPropertyAccessException) + + @Test fun assertionError() = testCreateException(::AssertionError, ::AssertionError, ::AssertionError) + + + private fun testCreateException(noarg: () -> T, fromMessage: (String?) -> T, fromCause: ((Throwable?) -> T)? = null, fromMessageCause: ((String?, Throwable?) -> T)? = null) { + noarg().let { e -> + assertEquals(null, e.message) + assertEquals(null, e.cause) + } + + fromMessage("message").let { e -> + assertEquals("message", e.message) + assertEquals(null, e.cause) + } + + fromMessage(null).let { e -> + assertTrue(e.message == null || e.message == "null") + } + + fromMessageCause?.run { + invoke("message", cause).let { e -> + assertEquals("message", e.message) + assertSame(cause, e.cause) + } + invoke(null, null).let { e -> + assertEquals(null, e.message) + assertEquals(null, e.cause) + } + } + + fromCause?.invoke(cause)?.let { e -> + assertSame(cause, e.cause) + } + } + +} \ No newline at end of file