Kotlin Tutorial
Kotlin Useful Resources
Selected Reading
- Kotlin - Exception Handling
- Kotlin - Destructuring Declarations
- Kotlin - Delegation
- Kotlin - Generics
- Kotlin - Sealed Class
- Kotlin - Data Classes
- Kotlin - Extension
- Kotlin - Visibility Control
- Kotlin - Interface
- Kotlin - Abstract Classes
- Kotlin - Inheritance
- Kotlin - Constructors
- Kotlin - Class and Objects
- Kotlin - Maps
- Kotlin - Sets
- Kotlin - Lists
- Kotlin - Collections
- Kotlin - Break and Continue
- Kotlin - While Loop
- Kotlin - For Loop
- Kotlin - When Expression
- Kotlin - if...Else Expression
- Kotlin - Control Flow
- Kotlin - Functions
- Kotlin - Ranges
- Kotlin - Arrays
- Kotlin - Strings
- Kotlin - Booleans
- Kotlin - Operators
- Kotlin - Data Types
- Kotlin - Variables
- Kotlin - Keywords
- Kotlin - Comments
- Kotlin - Basic Syntax
- Kotlin - Architecture
- Kotlin - Environment Setup
- Kotlin - Overview
- Kotlin - Home
Kotlin Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Kotlin - Exception Handling
Kotpn - Exception Handpng
Exception handpng is a very important part of a programming language. This technique restricts our apppcation from generating the wrong output at runtime. In this chapter, we will learn how to handle runtime exception in Kotpn. The exceptions in Kotpn is pretty similar to the exceptions in Java. All the exceptions are descendants of the “Throwable” class. Following example shows how to use exception handpng technique in Kotpn.
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 Handepng in Kotpn"); } }
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 Handepng in Kotpn
Note − Like Java, Kotpn also executes the finally block after executing the catch block.
Advertisements