From the course: Kotlin Essential Training: Functions, Collections, and I/O
Unlock the full course today
Join today to access over 24,700 courses taught by industry experts.
try/catch as control flow - Kotlin Tutorial
From the course: Kotlin Essential Training: Functions, Collections, and I/O
try/catch as control flow
- [Instructor] I don't know about you, but sometimes, maybe even most of the time, my code has bugs in it. Sometimes those bugs throw exceptions which may crash our programs. These can stem from things like trying to perform an operation on a null value or trying to divide by zero. There are many ways to work towards preventing these errors and one of which is try catch. We're going to continue our exploration of expressions in Kotlin by looking at how we can use try catch as an expression to write more concise, error safe code. So let's take a look at an example here. Val message = "the value is ${10 / 0}". What do you think will happen if we run this? If you guessed that our code will crash, you are correct. Dividing by zero throws an arithmetic exception which crashes our application when we run it. Try catch lets us catch these thrown exceptions without crashing and respond to them in some useful way. If we wrap…
Contents
-
-
-
-
-
-
(Locked)
if and when statements12m 55s
-
(Locked)
Statements vs. expressions7m 19s
-
(Locked)
try/catch as control flow6m 25s
-
(Locked)
Smart casting11m 44s
-
(Locked)
while loops5m 17s
-
(Locked)
for loops3m 49s
-
(Locked)
Challenge: Replace an if/else expression with a when expression1m 12s
-
(Locked)
Solution: Replace an if/else expression with a when expression5m 15s
-
(Locked)
-
-
-
-
-