withTimeout() throws a TimeoutCancellationException. If you need to do work in case of a timeout, you can wrap the withTimeout() in try/catch and catch the TimeoutCancellationException.

In the previous lesson, if you run the code, you never see the "This is printed after the timeout" message. That is because withTimeout() threw the TimeoutCancellationException. That was caught by the launch() code, bypassing our "after the timeout" println() call. However, in this lesson, since we are catching the TimeoutCancellationException, you will see both the "We got a timeout exception" message and the "This is printed after the timeout" message.

You can learn more about this in:
Run Edit