YSHUSH

Exception 본문

Coding/Kotlin

Exception

코딩; 2022. 1. 25. 16:13

 

try catch finally
val a = 6
val b = 0
var c:Int

try {
    c = a / b
}catch (e:ArithmeticException){
    println("${e.message}")
}catch(e: Exception){
    println("${e.message}")
}finally{
    println("finally")
}
println("program end")
fun main(args: Array<String>) {

var amount = 600

    try {
        amount -= 100
        checkAmount(amount)
    }catch (e:Exception) {
        println(e.message)
    }
    println("program end")
}
fun checkAmount(amount: Int){
    if(amount < 1000){
        throw Exception("잔고가 $amount 로 1000이하 입니다.")
    }
}

'Coding > Kotlin' 카테고리의 다른 글

Default parameters(기본인수)  (0) 2022.01.25
함수  (0) 2022.01.25
미니게임(Random Number 찾기)  (0) 2022.01.24
제어문(if, for, switch, while)  (0) 2022.01.24
배열  (0) 2022.01.24