1. try-catch-finally 구문

java와 동일

fun parseIntOrThrow(str: String): Int {
	try {
		return str.toInt() // toInt 형변한 함수 사용
	} catch (e: NumberFormatException) {
		throw IllegalArgumentException("주어진 ${str}는 숫자가 아니다") // new 없음
	}
}
// if-else처럼 try-catch도 expression로 간주
fun parseIntOrThrowV2(str: String): Int? {
	return try {
		str.toInt()
	} catch (e: NumberFormatException) {
		null
	}
}
  1. Checked Exception과 Unchecked Exception

Checked Exception : throws같은거

java와 다른점 ⇒ throws를 통해서 이거는 무조건 예외가 난다는 것을 메소드 시그니처에 명시하지 않는다.

모두 Unchecked Exception이다.

void test() throws new Exception()
  1. try with resources

코틀린에 try with resources 구문은 없다.

코틀린에 문법에 맞게 바꾼 use함수가 있는데 이는 17강에서 설명한다.

<aside> 💡 try catch finally 구문은 문법적으로 완전히 동일하다. Kotlin에서는 try catch가 expression이다. Kotlin에서 모든 예외는 Unchecked Expression이다. Kotlin에서는 try with resources 구문이 없다.

</aside>