diff --git a/compiler/testData/codegen/box/binaryOp/divisionByZero.kt b/compiler/testData/codegen/box/binaryOp/divisionByZero.kt index 09b41c8fec1..92d44f6d143 100644 --- a/compiler/testData/codegen/box/binaryOp/divisionByZero.kt +++ b/compiler/testData/codegen/box/binaryOp/divisionByZero.kt @@ -1,15 +1,25 @@ // IGNORE_BACKEND: JS_IR // IGNORE_BACKEND: JS -// reason - no ArithmeticException in JS +// reason - no error from division by zero in JS + +fun expectFail(f: () -> Unit): Nothing? { + try { + f() + } catch (e: ArithmeticException) { + return null + } + throw AssertionError("Expected ArithmeticException to be thrown") +} + fun box(): String { val a1 = 0 - val a2 = try { 1 / 0 } catch(e: ArithmeticException) { 0 } - val a3 = try { 1 / a1 } catch(e: ArithmeticException) { 0 } - val a4 = try { 1 / a2 } catch(e: ArithmeticException) { 0 } - val a5 = try { 2 * (1 / 0) } catch(e: ArithmeticException) { 0 } - val a6 = try { 2 * 1 / 0 } catch(e: ArithmeticException) { 0 } + val a2 = expectFail { 1 / 0 } ?: 0 + val a3 = expectFail { 1 / a1 } ?: 0 + val a4 = expectFail { 1 / a2 } ?: 0 + val a5 = expectFail { 2 * (1 / 0) } ?: 0 + val a6 = expectFail { 2 * 1 / 0 } ?: 0 - try { val s1 = "${2 * (1 / 0) }" } catch(e: ArithmeticException) { } + val s1 = expectFail { "${2 * (1 / 0) }" } ?: "OK" - return "OK" + return s1 } \ No newline at end of file diff --git a/libraries/stdlib/common/src/kotlin/ExceptionsH.kt b/libraries/stdlib/common/src/kotlin/ExceptionsH.kt index ab9f9ad01c1..567441dbe7c 100644 --- a/libraries/stdlib/common/src/kotlin/ExceptionsH.kt +++ b/libraries/stdlib/common/src/kotlin/ExceptionsH.kt @@ -88,6 +88,12 @@ public expect open class NoSuchElementException : RuntimeException { constructor(message: String?) } +@SinceKotlin("1.3") +public expect open class ArithmeticException : RuntimeException { + constructor() + constructor(message: String?) +} + @Deprecated("This exception type is not supposed to be thrown or caught in common code and will be removed from kotlin-stdlib-common soon.") public expect open class NoWhenBranchMatchedException : RuntimeException { constructor() diff --git a/libraries/stdlib/js/src/kotlin/exceptions.kt b/libraries/stdlib/js/src/kotlin/exceptions.kt index 17fe7824827..37e1e060c62 100644 --- a/libraries/stdlib/js/src/kotlin/exceptions.kt +++ b/libraries/stdlib/js/src/kotlin/exceptions.kt @@ -107,6 +107,10 @@ public actual open class NoSuchElementException actual constructor(message: Stri actual constructor() : this(null) } +@SinceKotlin("1.3") +public actual open class ArithmeticException actual constructor(message: String?) : RuntimeException(message) { + actual constructor() : this(null) +} public actual open class NoWhenBranchMatchedException actual constructor(message: String?, cause: Throwable?) : RuntimeException(message, cause) { actual constructor() : this(null, null) diff --git a/libraries/stdlib/jvm/runtime/kotlin/TypeAliases.kt b/libraries/stdlib/jvm/runtime/kotlin/TypeAliases.kt index 0367d95216e..52c3e525c39 100644 --- a/libraries/stdlib/jvm/runtime/kotlin/TypeAliases.kt +++ b/libraries/stdlib/jvm/runtime/kotlin/TypeAliases.kt @@ -16,6 +16,7 @@ package kotlin @SinceKotlin("1.1") public actual typealias IndexOutOfBoundsException = java.lang.IndexOutOfBoundsException @SinceKotlin("1.1") public actual typealias UnsupportedOperationException = java.lang.UnsupportedOperationException +@SinceKotlin("1.3") public actual typealias ArithmeticException = java.lang.ArithmeticException @SinceKotlin("1.1") public actual typealias NumberFormatException = java.lang.NumberFormatException @SinceKotlin("1.1") public actual typealias NullPointerException = java.lang.NullPointerException @SinceKotlin("1.1") public actual typealias ClassCastException = java.lang.ClassCastException @@ -23,5 +24,4 @@ package kotlin @SinceKotlin("1.1") public actual typealias NoSuchElementException = java.util.NoSuchElementException - @SinceKotlin("1.1") public actual typealias Comparator = java.util.Comparator diff --git a/libraries/stdlib/test/ExceptionTest.kt b/libraries/stdlib/test/ExceptionTest.kt index 814cb2cea49..78cfc996911 100644 --- a/libraries/stdlib/test/ExceptionTest.kt +++ b/libraries/stdlib/test/ExceptionTest.kt @@ -22,6 +22,7 @@ class ExceptionTest { @Test fun nullPointerException() = testCreateException(::NullPointerException, ::NullPointerException) @Test fun classCastException() = testCreateException(::ClassCastException, ::ClassCastException) @Test fun noSuchElementException() = testCreateException(::NoSuchElementException, ::NoSuchElementException) + @Test fun arithmeticException() = testCreateException(::ArithmeticException, ::ArithmeticException) @Test fun noWhenBranchMatchedException() = testCreateException(::NoWhenBranchMatchedException, ::NoWhenBranchMatchedException, ::NoWhenBranchMatchedException, ::NoWhenBranchMatchedException) @Test fun uninitializedPropertyAccessException() = testCreateException(::UninitializedPropertyAccessException, ::UninitializedPropertyAccessException, ::UninitializedPropertyAccessException, ::UninitializedPropertyAccessException)