Add ir interpreter tests
This commit is contained in:
committed by
TeamCityServer
parent
cc2d7340dc
commit
e28ab45c51
@@ -0,0 +1,88 @@
|
||||
@CompileTimeCalculation
|
||||
fun throwExample(a: Int, b: Int): Int {
|
||||
if (b == 0) throw ArithmeticException("Divide by zero")
|
||||
return a / b
|
||||
}
|
||||
|
||||
@CompileTimeCalculation
|
||||
fun throwNullMessage(a: Int, b: Int): Int {
|
||||
if (b == 0) throw ArithmeticException(null)
|
||||
return a / b
|
||||
}
|
||||
|
||||
const val a1 = <!EVALUATED: `false`!>try {
|
||||
throwExample(10, 0)
|
||||
true
|
||||
} catch (e: ArithmeticException) {
|
||||
false
|
||||
}<!>
|
||||
const val a2 = <!EVALUATED: `true`!>try {
|
||||
throwExample(10, 1)
|
||||
true
|
||||
} catch (e: ArithmeticException) {
|
||||
false
|
||||
}<!>
|
||||
|
||||
const val b1 = <!EVALUATED: `false`!>try {
|
||||
throwExample(10, 0)
|
||||
true
|
||||
} catch (e: Exception) {
|
||||
false
|
||||
}<!>
|
||||
const val b2 = <!EVALUATED: `true`!>try {
|
||||
throwExample(10, 1)
|
||||
true
|
||||
} catch (e: Exception) {
|
||||
false
|
||||
}<!>
|
||||
const val b3 = <!EVALUATED: `false`!>try {
|
||||
throwExample(10, 0)
|
||||
true
|
||||
} catch (e: Throwable) {
|
||||
false
|
||||
}<!>
|
||||
|
||||
const val c1 = <!EVALUATED: `1`!>try {
|
||||
throwExample(10, 0)
|
||||
0
|
||||
} catch (e: ArithmeticException) {
|
||||
1
|
||||
} catch (e: Exception) {
|
||||
2
|
||||
}<!>
|
||||
const val c2 = <!EVALUATED: `1`!>try {
|
||||
throwExample(10, 0)
|
||||
0
|
||||
} catch (e: Exception) {
|
||||
1
|
||||
} catch (e: ArithmeticException) {
|
||||
2
|
||||
}<!>
|
||||
const val c3 = <!EVALUATED: `2`!>try {
|
||||
throwExample(10, 0)
|
||||
0
|
||||
} catch (e: NullPointerException) {
|
||||
1
|
||||
} catch (e: ArithmeticException) {
|
||||
2
|
||||
}<!>
|
||||
|
||||
const val d1 = <!EVALUATED: `Divide by zero`!>try {
|
||||
throwExample(10, 0)
|
||||
"Without exception"
|
||||
} catch (e: ArithmeticException) {
|
||||
e.message ?: "Exception without message"
|
||||
}<!>
|
||||
const val d2 = <!EVALUATED: `Without exception`!>try {
|
||||
throwExample(10, 1)
|
||||
"Without exception"
|
||||
} catch (e: ArithmeticException) {
|
||||
e.message ?: "Exception without message"
|
||||
}<!>
|
||||
|
||||
const val nullMessage = <!EVALUATED: `Exception without message`!>try {
|
||||
throwNullMessage(10, 0)
|
||||
"Without exception"
|
||||
} catch (e: ArithmeticException) {
|
||||
e.message ?: "Exception without message"
|
||||
}<!>
|
||||
@@ -0,0 +1,28 @@
|
||||
@CompileTimeCalculation
|
||||
fun tryCatch(integer: Int): Boolean {
|
||||
try {
|
||||
val a = 10 / integer
|
||||
return true
|
||||
} catch (e: ArithmeticException) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
const val a1 = <!EVALUATED: `false`!>tryCatch(0)<!>
|
||||
const val a2 = <!EVALUATED: `true`!>tryCatch(1)<!>
|
||||
const val a3 = <!EVALUATED: `true`!>tryCatch(100)<!>
|
||||
|
||||
@CompileTimeCalculation
|
||||
fun multiTryCatch(integer: Int): String {
|
||||
return try {
|
||||
val a = 10 / integer
|
||||
"Normal"
|
||||
} catch (e: AssertionError) {
|
||||
"AssertionError"
|
||||
} catch (e: ArithmeticException) {
|
||||
"ArithmeticException"
|
||||
}
|
||||
}
|
||||
|
||||
const val b1 = <!EVALUATED: `ArithmeticException`!>multiTryCatch(0)<!>
|
||||
const val b2 = <!EVALUATED: `Normal`!>multiTryCatch(1)<!>
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
@CompileTimeCalculation
|
||||
fun tryCatch(integer: Int): String {
|
||||
var str = ""
|
||||
try {
|
||||
str += "Start dividing\n"
|
||||
val a = 10 / integer
|
||||
str += "Without exception\n"
|
||||
} catch (e: ArithmeticException) {
|
||||
str += "Exception\n"
|
||||
} finally {
|
||||
str += "Finally\n"
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
const val a1 = <!EVALUATED: `Start dividing
|
||||
Exception
|
||||
Finally
|
||||
`!>tryCatch(0)<!>
|
||||
const val a2 = <!EVALUATED: `Start dividing
|
||||
Without exception
|
||||
Finally
|
||||
`!>tryCatch(1)<!>
|
||||
const val a3 = <!EVALUATED: `Start dividing
|
||||
Without exception
|
||||
Finally
|
||||
`!>tryCatch(100)<!>
|
||||
@@ -0,0 +1,73 @@
|
||||
import kotlin.*
|
||||
import kotlin.collections.*
|
||||
|
||||
@CompileTimeCalculation
|
||||
fun classCastWithException(a: Any): String {
|
||||
return try {
|
||||
a as Int
|
||||
"Given value is $a and its doubled value is ${2 * a}"
|
||||
} catch (e: ClassCastException) {
|
||||
"Given value isnt't Int; Exception message: \"${e.message}\""
|
||||
}
|
||||
}
|
||||
|
||||
@CompileTimeCalculation
|
||||
fun safeClassCast(a: Any): Int {
|
||||
return (a as? String)?.length ?: -1
|
||||
}
|
||||
|
||||
@CompileTimeCalculation
|
||||
fun <T> unsafeClassCast(): T {
|
||||
return 1 as T
|
||||
}
|
||||
|
||||
@CompileTimeCalculation
|
||||
fun <T> getIntList() = listOf<Int>(1, 2) as T
|
||||
|
||||
@CompileTimeCalculation
|
||||
fun <T> getStringNullableList() = listOf<String?>(null, "1") as T
|
||||
|
||||
@CompileTimeCalculation
|
||||
fun getLength(str: String) = str.length
|
||||
|
||||
@CompileTimeCalculation
|
||||
class A<T>() {
|
||||
fun unsafeCast(): T {
|
||||
return 1 as T
|
||||
}
|
||||
}
|
||||
|
||||
const val a1 = <!EVALUATED: `Given value is 10 and its doubled value is 20`!>classCastWithException(10)<!>
|
||||
const val a2 = <!EVALUATED: `Given value isnt't Int; Exception message: "kotlin.String cannot be cast to kotlin.Int"`!>classCastWithException("10")<!>
|
||||
|
||||
const val b1 = <!EVALUATED: `-1`!>safeClassCast(10)<!>
|
||||
const val b2 = <!EVALUATED: `2`!>safeClassCast("10")<!>
|
||||
|
||||
// in this example all unsafe cast will be "successful", but will fall when trying to assign
|
||||
const val c1 = <!EVALUATED: `1`!>unsafeClassCast<Int>()<!>
|
||||
const val c2 = <!WAS_NOT_EVALUATED: `
|
||||
Exception java.lang.ClassCastException: kotlin.Int cannot be cast to kotlin.String
|
||||
at ClassCastExceptionKt.unsafeClassCast(classCastException.kt:21)
|
||||
at ClassCastExceptionKt.<clinit>(classCastException.kt:48)`!>unsafeClassCast<String>()<!>
|
||||
|
||||
const val d1 = A<Int>().<!EVALUATED: `1`!>unsafeCast()<!>
|
||||
const val d2 = A<String>().<!WAS_NOT_EVALUATED: `
|
||||
Exception java.lang.ClassCastException: kotlin.Int cannot be cast to kotlin.String
|
||||
at ClassCastExceptionKt.A.unsafeCast(classCastException.kt:36)
|
||||
at ClassCastExceptionKt.<clinit>(classCastException.kt:51)`!>unsafeCast()<!>
|
||||
|
||||
const val stringList = getIntList<List<String>>().<!WAS_NOT_EVALUATED: `
|
||||
Exception java.lang.ClassCastException: kotlin.Int cannot be cast to kotlin.String
|
||||
at ClassCastExceptionKt.stringList.<anonymous>(classCastException.kt:54)
|
||||
at StandardKt.kotlin.let(Standard.kt:32)
|
||||
at ClassCastExceptionKt.<clinit>(classCastException.kt:53)`!>let {
|
||||
it[0].length
|
||||
}<!>
|
||||
const val nullableStringList = getStringNullableList<List<String>>().<!WAS_NOT_EVALUATED: `
|
||||
Exception java.lang.NullPointerException
|
||||
at ClassCastExceptionKt.nullableStringList.<anonymous>(classCastException.kt)
|
||||
at StandardKt.kotlin.let(Standard.kt:32)
|
||||
at ClassCastExceptionKt.<clinit>(classCastException.kt:56)`!>let { it[0].length }<!>
|
||||
const val nullableStringLength = <!WAS_NOT_EVALUATED: `
|
||||
Exception java.lang.IllegalArgumentException: Parameter specified as non-null is null: method ClassCastExceptionKt.getLength, parameter str
|
||||
at ClassCastExceptionKt.<clinit>(classCastException.kt:31)`!>getLength(getStringNullableList<List<String>>()[0])<!>
|
||||
@@ -0,0 +1,10 @@
|
||||
@CompileTimeCalculation
|
||||
fun infinityWhile(): Int {
|
||||
while (true) {}
|
||||
return 0
|
||||
}
|
||||
|
||||
const val a = <!WAS_NOT_EVALUATED: `
|
||||
Exception org.jetbrains.kotlin.ir.interpreter.exceptions.InterpreterTimeOutError: Exceeded execution limit of constexpr expression
|
||||
at CommandsOutExceptionKt.infinityWhile(commandsOutException.kt:3)
|
||||
at CommandsOutExceptionKt.<clinit>(commandsOutException.kt:7)`!>infinityWhile()<!>
|
||||
@@ -0,0 +1,15 @@
|
||||
const val a = <!EVALUATED: `1`!>try {
|
||||
10 / 0
|
||||
0
|
||||
} catch (e: RuntimeException) {
|
||||
1
|
||||
}<!>
|
||||
|
||||
const val b = <!WAS_NOT_EVALUATED: `
|
||||
Exception java.lang.ArithmeticException: / by zero
|
||||
at DivideByZeroKt.<clinit>(divideByZero.kt:8)`!>10 / 0<!>
|
||||
|
||||
// not working for now, but maybe will be supported
|
||||
//fun someFunWithCompileTimeInside() {
|
||||
// val exceptionExpected = 1 / 0
|
||||
//}
|
||||
@@ -0,0 +1,12 @@
|
||||
@CompileTimeCalculation
|
||||
fun append(sb: StringBuilder, value: CharSequence, start: Int, end: Int): String {
|
||||
return sb.append(value, start, end).toString()
|
||||
}
|
||||
|
||||
const val a = <!EVALUATED: `Some string with not zero length!!!`!>append(StringBuilder("Some string with not zero length"), "!!!", 0, 3)<!>
|
||||
const val b = <!WAS_NOT_EVALUATED: `
|
||||
Exception java.lang.IndexOutOfBoundsException: start -1, end 0, s.length() 3
|
||||
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:539)
|
||||
at java.lang.StringBuilder.append(StringBuilder.java:175)
|
||||
at ExceptionFromWrapperKt.append(exceptionFromWrapper.kt:3)
|
||||
at ExceptionFromWrapperKt.<clinit>(exceptionFromWrapper.kt:7)`!>append(StringBuilder("Some string with not zero length"), "!!!", -1, 0)<!>
|
||||
@@ -0,0 +1,49 @@
|
||||
// https://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Throwable.html#printStackTrace()
|
||||
@CompileTimeCalculation
|
||||
class HighLevelException(cause: Throwable?) : Exception(cause)
|
||||
@CompileTimeCalculation
|
||||
class MidLevelException(cause: Throwable?) : Exception(cause)
|
||||
@CompileTimeCalculation
|
||||
class LowLevelException : Exception()
|
||||
|
||||
@CompileTimeCalculation
|
||||
class Junk {
|
||||
public fun a(): Nothing {
|
||||
try {
|
||||
b()
|
||||
} catch (e: MidLevelException) {
|
||||
throw HighLevelException(e)
|
||||
}
|
||||
}
|
||||
|
||||
private fun b(): Nothing = c()
|
||||
|
||||
private fun c(): Nothing {
|
||||
try {
|
||||
d()
|
||||
} catch (e: LowLevelException) {
|
||||
throw MidLevelException(e)
|
||||
}
|
||||
}
|
||||
|
||||
private fun d(): Nothing = e()
|
||||
|
||||
private fun e(): Nothing = throw LowLevelException()
|
||||
}
|
||||
|
||||
const val exceptionWithCause = Junk().a().<!WAS_NOT_EVALUATED: `
|
||||
Exception HighLevelException: MidLevelException: LowLevelException
|
||||
at ExceptionWithCauseKt.Junk.a(exceptionWithCause.kt:15)
|
||||
at ExceptionWithCauseKt.<clinit>(exceptionWithCause.kt:34)
|
||||
Caused by: MidLevelException: LowLevelException
|
||||
at ExceptionWithCauseKt.Junk.c(exceptionWithCause.kt:25)
|
||||
at ExceptionWithCauseKt.Junk.b(exceptionWithCause.kt:19)
|
||||
at ExceptionWithCauseKt.Junk.a(exceptionWithCause.kt:13)
|
||||
at ExceptionWithCauseKt.<clinit>(exceptionWithCause.kt:34)
|
||||
Caused by: LowLevelException
|
||||
at ExceptionWithCauseKt.Junk.e(exceptionWithCause.kt:31)
|
||||
at ExceptionWithCauseKt.Junk.d(exceptionWithCause.kt:29)
|
||||
at ExceptionWithCauseKt.Junk.c(exceptionWithCause.kt:23)
|
||||
at ExceptionWithCauseKt.Junk.b(exceptionWithCause.kt:19)
|
||||
at ExceptionWithCauseKt.Junk.a(exceptionWithCause.kt:13)
|
||||
at ExceptionWithCauseKt.<clinit>(exceptionWithCause.kt:34)`!>toString()<!>
|
||||
@@ -0,0 +1,22 @@
|
||||
import kotlin.*
|
||||
|
||||
@CompileTimeCalculation
|
||||
fun getArrayElement(array: IntArray, index: Int): Int {
|
||||
try {
|
||||
return array[index]
|
||||
} catch (e: IndexOutOfBoundsException) {
|
||||
throw IllegalArgumentException("Index must be less than array size and greater than zero", e)
|
||||
}
|
||||
}
|
||||
|
||||
const val a = <!EVALUATED: `-1`!>try {
|
||||
getArrayElement(intArrayOf(1, 2, 3), -1).let { "Element at given index is " + it }
|
||||
} catch (e: Exception) {
|
||||
e.cause?.message ?: "Exception without message"
|
||||
}<!>
|
||||
|
||||
const val b = <!EVALUATED: `Element at given index is 1`!>try {
|
||||
getArrayElement(intArrayOf(1, 2, 3), 0).let { "Element at given index is " + it }
|
||||
} catch (e: Exception) {
|
||||
e.cause?.message ?: "Exception without message"
|
||||
}<!>
|
||||
@@ -0,0 +1,20 @@
|
||||
// FILE: main.kt
|
||||
|
||||
@CompileTimeCalculation
|
||||
fun callToOtherFile(mustThrowException: Boolean, message: String): Int {
|
||||
if (mustThrowException) throwException(message)
|
||||
return 0
|
||||
}
|
||||
|
||||
const val a = <!WAS_NOT_EVALUATED: `
|
||||
Exception java.lang.Exception: Exception from file1.kt
|
||||
at File1Kt.throwException(file1.kt:19)
|
||||
at MainKt.callToOtherFile(main.kt:5)
|
||||
at MainKt.<clinit>(main.kt:9)`!>callToOtherFile(true, "Exception from file1.kt")<!>
|
||||
|
||||
// FILE: file1.kt
|
||||
|
||||
@CompileTimeCalculation
|
||||
fun throwException(message: String) {
|
||||
throw Exception(message)
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
@CompileTimeCalculation
|
||||
class A
|
||||
|
||||
@CompileTimeCalculation
|
||||
fun notNullAssertion(value: Int?): String {
|
||||
return try {
|
||||
value!!
|
||||
"Value isn't null"
|
||||
} catch (e: NullPointerException) {
|
||||
"Value is null"
|
||||
}
|
||||
}
|
||||
|
||||
@CompileTimeCalculation
|
||||
fun notNullAssertionForObject(value: A?): String {
|
||||
return try {
|
||||
value!!
|
||||
"Value isn't null"
|
||||
} catch (e: NullPointerException) {
|
||||
"Value is null"
|
||||
}
|
||||
}
|
||||
|
||||
@CompileTimeCalculation
|
||||
fun notNullAssertionForSomeWrapper(value: StringBuilder?): String {
|
||||
return try {
|
||||
value!!.toString()
|
||||
} catch (e: NullPointerException) {
|
||||
"Value is null"
|
||||
}
|
||||
}
|
||||
|
||||
@CompileTimeCalculation
|
||||
fun notNullLambda(lambda: (() -> String)?): String {
|
||||
return when {
|
||||
lambda != null -> lambda()
|
||||
else -> "Lambda is null"
|
||||
}
|
||||
}
|
||||
|
||||
const val a1 = <!EVALUATED: `Value isn't null`!>notNullAssertion(1)<!>
|
||||
const val a2 = <!EVALUATED: `Value is null`!>notNullAssertion(null)<!>
|
||||
const val b1 = <!EVALUATED: `Value isn't null`!>notNullAssertionForObject(A())<!>
|
||||
const val b2 = <!EVALUATED: `Value is null`!>notNullAssertionForObject(null)<!>
|
||||
const val c1 = <!EVALUATED: `Some text`!>notNullAssertionForSomeWrapper(StringBuilder("Some text"))<!>
|
||||
const val c2 = <!EVALUATED: `Value is null`!>notNullAssertionForSomeWrapper(null)<!>
|
||||
const val d1 = <!EVALUATED: `Not null lambda`!>notNullLambda { "Not null lambda" }<!>
|
||||
const val d2 = <!EVALUATED: `Lambda is null`!>notNullLambda(null)<!>
|
||||
@@ -0,0 +1,22 @@
|
||||
@CompileTimeCalculation
|
||||
fun foo(i: Int): Int = foo(i + 1)
|
||||
const val overflow = <!WAS_NOT_EVALUATED: `
|
||||
Exception java.lang.StackOverflowError
|
||||
at StackOverflowKt.foo(stackOverflow.kt:1)
|
||||
at StackOverflowKt.foo(stackOverflow.kt:2)
|
||||
at StackOverflowKt.foo(stackOverflow.kt:2)
|
||||
at StackOverflowKt.foo(stackOverflow.kt:2)
|
||||
at StackOverflowKt.foo(stackOverflow.kt:2)
|
||||
at StackOverflowKt.foo(stackOverflow.kt:2)
|
||||
at StackOverflowKt.foo(stackOverflow.kt:2)
|
||||
at StackOverflowKt.foo(stackOverflow.kt:2)
|
||||
at StackOverflowKt.foo(stackOverflow.kt:2)
|
||||
at StackOverflowKt.foo(stackOverflow.kt:2)
|
||||
...`!>foo(0)<!>
|
||||
|
||||
@CompileTimeCalculation
|
||||
fun withPossibleOverflow(x: Int): Int {
|
||||
if (x == 0) return 0
|
||||
return withPossibleOverflow(x - 1) + 1
|
||||
}
|
||||
const val notOverflow = <!EVALUATED: `5000`!>withPossibleOverflow(5_000)<!>
|
||||
@@ -0,0 +1,27 @@
|
||||
@CompileTimeCalculation
|
||||
fun divide(a: Int, b: Int) = a / b
|
||||
|
||||
@CompileTimeCalculation
|
||||
fun echo(a : Int, b: Int) = divide(a, b)
|
||||
|
||||
@CompileTimeCalculation
|
||||
fun getLengthOrThrowException(str: String?): Int {
|
||||
try {
|
||||
return str!!.length
|
||||
} catch (e: NullPointerException) {
|
||||
throw UnsupportedOperationException(e)
|
||||
}
|
||||
}
|
||||
|
||||
const val a = <!WAS_NOT_EVALUATED: `
|
||||
Exception java.lang.ArithmeticException: / by zero
|
||||
at StackTraceKt.divide(stackTrace.kt:2)
|
||||
at StackTraceKt.echo(stackTrace.kt:5)
|
||||
at StackTraceKt.<clinit>(stackTrace.kt:16)`!>echo(1, 0)<!>
|
||||
const val b = <!WAS_NOT_EVALUATED: `
|
||||
Exception java.lang.UnsupportedOperationException: java.lang.NullPointerException
|
||||
at StackTraceKt.getLengthOrThrowException(stackTrace.kt:12)
|
||||
at StackTraceKt.<clinit>(stackTrace.kt:17)
|
||||
Caused by: java.lang.NullPointerException
|
||||
at StackTraceKt.getLengthOrThrowException(stackTrace.kt:10)
|
||||
at StackTraceKt.<clinit>(stackTrace.kt:17)`!>getLengthOrThrowException(null)<!>
|
||||
@@ -0,0 +1,215 @@
|
||||
@CompileTimeCalculation
|
||||
fun tryFinally(initValue: Int): Int {
|
||||
var x = initValue
|
||||
try {
|
||||
return x
|
||||
} finally {
|
||||
x = x + 1 // result is never used
|
||||
}
|
||||
}
|
||||
|
||||
@CompileTimeCalculation
|
||||
fun tryFinally2(): String {
|
||||
var str: String = ""
|
||||
try {
|
||||
str += "Inside try block; "
|
||||
} finally {
|
||||
str += "Inside finally; "
|
||||
}
|
||||
str += "Outside try; "
|
||||
return str
|
||||
}
|
||||
|
||||
@CompileTimeCalculation
|
||||
fun outerReturnTryFinally(n: Int): Int {
|
||||
return try {
|
||||
n
|
||||
} finally {
|
||||
-1
|
||||
}
|
||||
}
|
||||
|
||||
@CompileTimeCalculation
|
||||
fun exceptionInFinally(divideBy: Int): Int {
|
||||
return try {
|
||||
0
|
||||
} finally {
|
||||
1 / divideBy
|
||||
}
|
||||
}
|
||||
|
||||
@CompileTimeCalculation
|
||||
fun tryCatchFinally(): Int {
|
||||
try {
|
||||
throw IllegalArgumentException("In try")
|
||||
} catch (e: IllegalArgumentException) {
|
||||
throw IllegalArgumentException("In catch")
|
||||
} finally {
|
||||
throw IllegalArgumentException("In finally")
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
@CompileTimeCalculation
|
||||
fun returnTryFinally(): String {
|
||||
return try { "OK" } finally { "NOT OK" } // result from finally is never used
|
||||
}
|
||||
|
||||
@CompileTimeCalculation
|
||||
fun tryCatchReturnFinally(divideBy: Int): Int {
|
||||
var y = 0
|
||||
try {
|
||||
1 / divideBy
|
||||
} catch (e: ArithmeticException) {
|
||||
return y
|
||||
} finally {
|
||||
y++
|
||||
}
|
||||
|
||||
return y
|
||||
}
|
||||
|
||||
@CompileTimeCalculation
|
||||
fun tryFinallyContinue(): Int {
|
||||
var y = 0
|
||||
while (y < 10) {
|
||||
try {
|
||||
continue
|
||||
} finally {
|
||||
y++
|
||||
}
|
||||
|
||||
throw IllegalStateException("\"continue\" must drop remaining frame")
|
||||
}
|
||||
|
||||
return y
|
||||
}
|
||||
|
||||
@CompileTimeCalculation
|
||||
fun tryFinallyBreak(): Int {
|
||||
var y = 0
|
||||
while (y < 10) {
|
||||
try {
|
||||
break
|
||||
} finally {
|
||||
y++
|
||||
}
|
||||
}
|
||||
|
||||
return y
|
||||
}
|
||||
|
||||
@CompileTimeCalculation
|
||||
fun tryCatchFinallyContinue(divideBy: Int): Int {
|
||||
var y = 0
|
||||
while (y < 10) {
|
||||
try {
|
||||
1 / divideBy
|
||||
} catch (e: ArithmeticException) {
|
||||
continue
|
||||
} finally {
|
||||
y++
|
||||
}
|
||||
|
||||
throw IllegalStateException("\"continue\" must drop remaining frame")
|
||||
}
|
||||
|
||||
return y
|
||||
}
|
||||
|
||||
@CompileTimeCalculation
|
||||
fun innerTryFinally(n: Int): Int {
|
||||
try {
|
||||
try {
|
||||
return 1
|
||||
} finally {
|
||||
n + 1
|
||||
}
|
||||
return 2
|
||||
} finally {
|
||||
n + 10
|
||||
}
|
||||
return 3
|
||||
}
|
||||
|
||||
@CompileTimeCalculation
|
||||
fun innerTryFinallyReturn(n: Int): Int {
|
||||
//return n / 0
|
||||
try {
|
||||
try {
|
||||
return 1
|
||||
} finally {
|
||||
n + 1
|
||||
}
|
||||
return 2
|
||||
} finally {
|
||||
return n + 10
|
||||
}
|
||||
return 3
|
||||
}
|
||||
|
||||
@CompileTimeCalculation
|
||||
fun tryCatch(n: Int): Int {
|
||||
return try {
|
||||
delete(n)
|
||||
} catch (e: ArithmeticException) {
|
||||
-1
|
||||
} finally {
|
||||
-2
|
||||
}
|
||||
}
|
||||
|
||||
@CompileTimeCalculation
|
||||
fun delete(n: Int): Int {
|
||||
return try {
|
||||
1 / n
|
||||
} catch (e: NullPointerException) {
|
||||
-1
|
||||
} finally {
|
||||
-2
|
||||
}
|
||||
}
|
||||
|
||||
@CompileTimeCalculation
|
||||
fun tryTryFinally(): Int {
|
||||
val zero = 0
|
||||
val nullable: Int? = null
|
||||
try {
|
||||
try {
|
||||
1 / zero
|
||||
} finally {
|
||||
nullable!!
|
||||
}
|
||||
} catch (e: NullPointerException) {
|
||||
return -1
|
||||
} finally {
|
||||
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
const val a1 = <!EVALUATED: `0`!>tryFinally(0)<!>
|
||||
const val a2 = <!EVALUATED: `10`!>tryFinally(10)<!>
|
||||
//const val a3 = outerReturnTryFinally(10) TODO -> `10`
|
||||
const val b1 = <!EVALUATED: `Inside try block; Inside finally; Outside try; `!>tryFinally2()<!>
|
||||
const val c1 = <!WAS_NOT_EVALUATED: `
|
||||
Exception java.lang.IllegalArgumentException: In finally
|
||||
at TryFinallyKt.tryCatchFinally(tryFinally.kt:48)
|
||||
at TryFinallyKt.<clinit>(tryFinally.kt:196)`!>tryCatchFinally()<!>
|
||||
const val c2 = <!EVALUATED: `0`!>exceptionInFinally(10)<!>
|
||||
const val c3 = <!WAS_NOT_EVALUATED: `
|
||||
Exception java.lang.ArithmeticException: / by zero
|
||||
at TryFinallyKt.exceptionInFinally(tryFinally.kt:37)
|
||||
at TryFinallyKt.<clinit>(tryFinally.kt:198)`!>exceptionInFinally(0)<!>
|
||||
//const val d1 = returnTryFinally() TODO -> `OK`
|
||||
const val d2 = <!EVALUATED: `1`!>tryCatchReturnFinally(10)<!>
|
||||
const val d3 = <!EVALUATED: `0`!>tryCatchReturnFinally(0)<!>
|
||||
const val e1 = <!EVALUATED: `10`!>tryFinallyContinue()<!>
|
||||
const val e2 = <!EVALUATED: `1`!>tryFinallyBreak()<!>
|
||||
const val e3 = <!EVALUATED: `10`!>tryCatchFinallyContinue(0)<!>
|
||||
//const val f1 = innerTryFinally(10) TODO -> `1`
|
||||
const val f2 = <!EVALUATED: `20`!>innerTryFinallyReturn(10)<!>
|
||||
//const val g1 = tryCatch(10) TODO -> `0`
|
||||
//const val g2 = tryCatch(0) TODO -> `-1`
|
||||
//const val h1 = tryTryFinally() TODO -> `-1`
|
||||
Reference in New Issue
Block a user