Complete the code to assign the result of the try block to the variable.
val result = try { 10 / 2 } catch (e: Exception) { 0 }
The try block must start with a curly brace { to define the block of code.
Complete the code to catch an exception and return -1.
val result = try { 10 / 0 } catch ([1]: Exception) { -1 }
The variable name for the caught exception is commonly e, but any valid identifier works. Here, e is used.
Fix the error in the try-catch expression to avoid throwing an exception.
val result = try { 10 / [1] } catch (e: Exception) { 0 }
Dividing by zero causes an exception. Using 2 avoids the exception and returns 5.
Complete the code to create a try-catch expression that returns the length of a string or -1 if null.
val length = try { input!!.length } catch (e: [1]) { -1 }
The try block must start with { and catch the NullPointerException to handle null input safely.
Fill both blanks to create a try-catch expression that returns the uppercase string or "ERROR" if exception occurs.
val output = try { input.toUpperCase() } catch (e: [1]) { [2] }
The try block starts with {, catches Exception, and returns the string "ERROR" on failure.