ExploringExceptions.kt 482 B

12345678910111213141516171819202122232425262728
  1. package com.virtualprogrammers.learning.kotlin
  2. import java.io.FileInputStream
  3. @Throws(InterruptedException::class)
  4. fun divide(a: Int, b: Int): Double {
  5. Thread.sleep(1000)
  6. return (a.toDouble() / b)
  7. }
  8. fun printFile() {
  9. val input = FileInputStream("file.txt")
  10. input.use {
  11. // an exception could be thrown
  12. }
  13. }
  14. fun main() {
  15. val result = try {
  16. divide(5, 23)
  17. } catch (e: Exception) {
  18. println(e)
  19. 0
  20. }
  21. println(result)
  22. }