Difference between revisions of "KOTLIN: Exception Handling"
Onnowpurbo (talk | contribs) (Created page with "Sumber: https://www.tutorialspoint.com/kotlin/kotlin_exception_handling.htm Exception handling is a very important part of a programming language. This technique restricts o...") |
Onnowpurbo (talk | contribs) |
||
Line 4: | Line 4: | ||
Exception handling is a very important part of a programming language. This technique restricts our application from generating the wrong output at runtime. In this chapter, we will learn how to handle runtime exception in Kotlin. The exceptions in Kotlin is pretty similar to the exceptions in Java. All the exceptions are descendants of the “Throwable” class. Following example shows how to use exception handling technique in Kotlin. | Exception handling is a very important part of a programming language. This technique restricts our application from generating the wrong output at runtime. In this chapter, we will learn how to handle runtime exception in Kotlin. The exceptions in Kotlin is pretty similar to the exceptions in Java. All the exceptions are descendants of the “Throwable” class. Following example shows how to use exception handling technique in Kotlin. | ||
− | fun main(args: Array<String>) { | + | fun main(args: Array<String>) { |
− | + | try { | |
− | + | val myVar:Int = 12; | |
− | + | val v:String = "Tutorialspoint.com"; | |
− | + | v.toInt(); | |
− | + | } catch(e:Exception) { | |
− | + | e.printStackTrace(); | |
− | + | } finally { | |
− | + | println("Exception Handeling in Kotlin"); | |
− | + | } | |
− | } | + | } |
+ | |||
In the above piece of code, we have declared a String and later tied that string into the integer, which is actually a runtime exception. Hence, we will get the following output in the browser. | In the above piece of code, we have declared a String and later tied that string into the integer, which is actually a runtime exception. Hence, we will get the following output in the browser. | ||
− | val myVar:Int = 12; | + | val myVar:Int = 12; |
− | Exception Handeling in Kotlin | + | |
+ | ==Exception Handeling in Kotlin== | ||
+ | |||
Note − Like Java, Kotlin also executes the finally block after executing the catch block. | Note − Like Java, Kotlin also executes the finally block after executing the catch block. | ||
Latest revision as of 10:00, 23 July 2022
Sumber: https://www.tutorialspoint.com/kotlin/kotlin_exception_handling.htm
Exception handling is a very important part of a programming language. This technique restricts our application from generating the wrong output at runtime. In this chapter, we will learn how to handle runtime exception in Kotlin. The exceptions in Kotlin is pretty similar to the exceptions in Java. All the exceptions are descendants of the “Throwable” class. Following example shows how to use exception handling technique in Kotlin.
fun main(args: Array<String>) { try { val myVar:Int = 12; val v:String = "Tutorialspoint.com"; v.toInt(); } catch(e:Exception) { e.printStackTrace(); } finally { println("Exception Handeling in Kotlin"); } }
In the above piece of code, we have declared a String and later tied that string into the integer, which is actually a runtime exception. Hence, we will get the following output in the browser.
val myVar:Int = 12;
Exception Handeling in Kotlin
Note − Like Java, Kotlin also executes the finally block after executing the catch block.