August 2018
Beginner
178 pages
4h 56m
English
With exception handling features of a language, you can deal with unpredicted situations or exceptional states that can occur while your code is executing. Kotlin is no different from other languages, like C# and Java and has the same keywords for handling exceptions. These are try, catch, and finally. With these keywords, you can execute a function or some other action which may fail, execute some code in case of failure, and do a cleanup of resources.
val file = File("foo")var stream: OutputStream? = nulltry { stream = file.outputStream() //do something with stream} catch (ex: FileNotFoundException) { println("File doesn't exist")} finally { if (stream != null) stream.close()}
When you detect an error, or an exceptional state ...
Read now
Unlock full access