Practice 50 Kotlin Error Handling Coding Questions, TechnoVlogs

Practice 50 Kotlin Error Handling Coding Questions


Q1. Write a Kotlin program to demonstrate how to handle a NumberFormatException when parsing an invalid string to an integer.
  Input: "abc"
  Expected Output: "Invalid number format"

Q2. Write a Kotlin program that uses try-catch to handle a division by zero exception.
  Input: 10 / 0
  Expected Output: "Error: Division by zero"

Q3. Write a Kotlin program that handles an IndexOutOfBoundsException when accessing an invalid index in a list.
  Input: List [1, 2, 3], Access index 5
  Expected Output: "Index out of bounds"

Q4. Write a Kotlin program to catch and handle a NullPointerException when trying to access a method on a null object.
  Input: val str: String? = null
  Expected Output: "NullPointerException occurred"

Q5. Write a Kotlin program to handle an IOException while trying to read from a non-existing file.
  Input: "non_existing_file.txt"
  Expected Output: "IOException occurred: File not found"

Q6. Write a Kotlin program to demonstrate the usage of finally block to ensure resources are released.
  Expected Output: "Resource closed"

Q7. Write a Kotlin program to handle an IllegalArgumentException when a negative value is passed to a method that only accepts positive values.
  Input: -5
  Expected Output: "IllegalArgumentException: Value must be positive"

Q8. Write a Kotlin program to handle an ArithmeticException while performing a calculation involving division by zero.
  Input: 50 / 0
  Expected Output: "Error: Division by zero"

Q9. Write a Kotlin program to use try-catch to catch a custom exception when an invalid age is entered.
  Input: -1 (invalid age)
  Expected Output: "Invalid age entered"

Q10. Write a Kotlin program to handle a FileNotFoundException when trying to open a file that doesn’t exist.
   Input: "invalid.txt"
   Expected Output: "File not found"

Q11. Write a Kotlin program that demonstrates how to throw and catch a custom exception in Kotlin.
   Input: throw CustomException("Custom error")
   Expected Output: "Custom error caught"

Q12. Write a Kotlin program to handle an exception thrown inside a lambda expression.
   Input: lambda { throw IllegalStateException() }
   Expected Output: "IllegalStateException caught"

Q13. Write a Kotlin program that uses try-catch to catch and handle a TimeoutException.
   Input: Simulating a timeout situation
   Expected Output: "Timeout occurred"

Q14. Write a Kotlin program to handle an ArrayIndexOutOfBoundsException when trying to access an element beyond array bounds.
   Input: Array [1, 2, 3], Access index 4
   Expected Output: "Array index out of bounds"

Q15. Write a Kotlin program to handle a SecurityException while trying to access a restricted resource.
   Expected Output: "SecurityException: Access denied"

Q16. Write a Kotlin program to demonstrate how to handle a ClassCastException.
   Input: val obj: Any = "String"; val num: Int = obj as Int
   Expected Output: "ClassCastException occurred"

Q17. Write a Kotlin program to handle IOException when trying to write to a read-only file.
   Expected Output: "IOException: Cannot write to file"

Q18. Write a Kotlin program that catches and handles a FileAlreadyExistsException.
   Input: Trying to create a file that already exists
   Expected Output: "File already exists"

Q19. Write a Kotlin program to catch and handle a NegativeArraySizeException when creating an array with a negative size.
   Input: val arr = IntArray(-5)
   Expected Output: "Negative array size exception"

Q20. Write a Kotlin program that handles an exception using runCatching and prints the result or exception.
   Input: runCatching { 10 / 0 }
   Expected Output: "Exception: Division by zero"

Q21. Write a Kotlin program to catch a NumberFormatException when converting a string with letters to an integer.
   Input: "abc"
   Expected Output: "Invalid number format"

Q22. Write a Kotlin program to catch a NullPointerException when calling a method on a nullable type.
   Input: val str: String? = null; str!!.length
   Expected Output: "NullPointerException caught"

Q23. Write a Kotlin program that handles IllegalStateException when performing an illegal operation on an object in an invalid state.
   Input: Operation on an object in an invalid state
   Expected Output: "Illegal state exception"

Q24. Write a Kotlin program to handle IOException while reading from a file that has read permissions.
   Expected Output: "Error while reading the file"

Q25. Write a Kotlin program to handle a RuntimeException when performing a runtime operation that is not allowed.
   Expected Output: "Runtime exception occurred"

Q26. Write a Kotlin program to demonstrate handling an exception in a try-catch-finally block.
   Input: A block that throws an exception
   Expected Output: "Exception handled, finally block executed"

Q27. Write a Kotlin program that uses require to validate a condition and throw an exception if it is not met.
   Input: require(age >= 0)
   Expected Output: "IllegalArgumentException: Age cannot be negative"

Q28. Write a Kotlin program to use check to ensure a condition is true and throw an exception otherwise.
   Input: check(age > 0)
   Expected Output: "IllegalStateException: Invalid state"

Q29. Write a Kotlin program to demonstrate how throw can be used to trigger a custom exception in Kotlin.
   Expected Output: "Custom exception triggered"

Q30. Write a Kotlin program to catch and handle a UnsupportedOperationException.
   Input: Trying to perform an unsupported operation
   Expected Output: "Unsupported operation"

Q31. Write a Kotlin program to catch an exception and use message property of exception.
   Input: try { throw IOException("File not found") }
   Expected Output: "IOException: File not found"

Q32. Write a Kotlin program to demonstrate how to use try-catch to handle multiple exceptions.
   Input: try { val num = 10 / 0 } catch (e: ArithmeticException) { println("Division error") }
   Expected Output: "Division error"

Q33. Write a Kotlin program to use throw to raise an exception from a method based on a condition.
   Input: throw IllegalArgumentException("Negative value not allowed")
   Expected Output: "IllegalArgumentException: Negative value not allowed"

Q34. Write a Kotlin program to catch a FileNotFoundException when trying to read from a non-existing file.
   Expected Output: "File not found exception"

Q35. Write a Kotlin program to catch ParseException when parsing an invalid date string.
   Input: "invalid-date"
   Expected Output: "ParseException occurred"

Q36. Write a Kotlin program to catch an Exception and display the exception message in a custom format.
   Input: val num = 10 / 0
   Expected Output: "Caught exception: Division by zero"

Q37. Write a Kotlin program to handle a SocketTimeoutException when a socket connection takes too long.
   Expected Output: "Socket timeout occurred"

Q38. Write a Kotlin program to handle an exception when performing a database operation with incorrect query syntax.
   Expected Output: "SQLException: Invalid query"

Q39. Write a Kotlin program to catch and handle JSONException when parsing invalid JSON.
   Expected Output: "JSONException: Invalid JSON"

Q40. Write a Kotlin program to catch ClassCastException when trying to cast an object to an incompatible type.
   Expected Output: "ClassCastException occurred"

Q41. Write a Kotlin program to demonstrate how to handle exceptions using runCatching and print success or failure.
   Input: runCatching { 5 / 0 }
   Expected Output: "Failure: ArithmeticException: / by zero"

Q42. Write a Kotlin program that demonstrates handling a ConcurrentModificationException while iterating through a collection.
   Expected Output: "ConcurrentModificationException caught"

Q43. Write a Kotlin program to catch and handle IllegalAccessException when accessing private fields.
   Expected Output: "IllegalAccessException caught"

Q44. Write a Kotlin program to catch and handle SQLException while executing an invalid SQL query.
   Expected Output: "SQLException occurred"

Q45. Write a Kotlin program to demonstrate how to log an exception using Logger.
   Expected Output: "Error: Exception logged"

Q46. Write a Kotlin program to handle a ParseException when parsing an invalid date.
   Expected Output: "Date parsing failed"

Q47. Write a Kotlin program to handle a JSONException when an API response does not match expected JSON structure.
   Expected Output: "JSON parsing failed"

Q48. Write a Kotlin program to demonstrate how to catch and handle IllegalStateException.
   Expected Output: "Illegal state exception handled"

Q49. Write a Kotlin program that shows how to handle a NoSuchElementException while trying to access an element from an empty collection.
   Expected Output: "No element found"

Q50. Write a Kotlin program to demonstrate catching an exception and retrying an operation.
   Expected Output: "Operation retried after failure"

Share on Social Media