0
0
Kotlinprogramming~10 mins

Try-catch as an expression in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Try-catch as an expression
Start
Try block runs
No Exception?
YesTry result used
End
Exception?
YesCatch block runs
Catch result used
End
The try block runs first. If no exception occurs, its result is used. If an exception happens, the catch block runs and its result is used instead.
Execution Sample
Kotlin
val result = try {
    "123".toInt()
} catch (e: NumberFormatException) {
    -1
}
println(result)
This code tries to convert a string to an integer. If it fails, it catches the exception and returns -1 instead.
Execution Table
StepActionEvaluationResult
1Execute try block: "123".toInt()No exception123
2Assign try block result to 'result'result = 123123
3Print 'result'Output123
💡 Try block succeeded, no exception thrown, result is 123
Variable Tracker
VariableStartAfter Step 1After Step 2Final
resultuninitializeduninitialized123123
Key Moments - 2 Insights
Why does the catch block not run when the string is "123"?
Because the try block converts "123" to integer successfully without throwing an exception, as shown in execution_table step 1.
What happens if the string cannot be converted to an integer?
The try block throws a NumberFormatException, so the catch block runs and returns -1 instead, replacing the try block result.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'result' after step 2?
A123
B-1
Cuninitialized
DException
💡 Hint
Check the 'Assign try block result to result' row in the execution_table.
At which step does the program print the output?
AStep 1
BStep 3
CStep 2
DNo print occurs
💡 Hint
Look for the 'Print result' action in the execution_table.
If the string was "abc" instead of "123", what would happen?
AProgram crashes without catch
BTry block returns "abc"
CCatch block runs and result is -1
DResult remains uninitialized
💡 Hint
Recall that "abc" cannot convert to Int, so exception triggers catch block.
Concept Snapshot
Try-catch in Kotlin can be used as an expression.
Syntax:
val result = try { /* code */ } catch(e: Exception) { /* fallback */ }
If try succeeds, its value is used.
If try throws, catch value is used instead.
This allows safe assignment with error handling in one expression.
Full Transcript
In Kotlin, try-catch can be used as an expression that returns a value. The try block runs first. If it completes without error, its result is assigned. If an exception occurs, the catch block runs and its result is assigned instead. For example, converting a string to an integer inside try returns the number if successful. If conversion fails, catch returns -1. This way, you can safely assign a value with error handling in one line. The execution table shows each step: running try, assigning result, and printing output. Variables track the value changes. Key moments clarify why catch runs only on exceptions. The quiz tests understanding of these steps and outcomes.