This video provides a compelling case for why explicit error handling with `std::expected` is replacing the unpredictable overhead of traditional exceptions. It marks a necessary transition toward more deterministic and readable modern C++ code.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
The Death of Exceptions in C++
Added:For a long time, exceptions were the way to handle errors in C++. However, more and more developers have been moving away from them in favor of the expected class in C++ 23 and static assertions.
Here are some of the biggest problems with exceptions. C++ exceptions often obscure control flow. This code looks linear, but in reality, it could exit here or here if an exception is thrown.
Unlike some other languages, C++ function signatures don't declare which exceptions they may throw, which sometimes leaves the user guessing.
There are scenarios where throwing an exception can be catastrophic. If you try to throw inside a destructor, it will terminate your program. This is because destructors are implicitly no except. If you try to throw inside a no except function, it will call terminate.
Lastly, throwing exceptions can be absurdly expensive.
Throwing an exception can be 100 to 1,000 times slower than a normal return.
Expected is a C++ 23 vocabulary type, which is designed for explicit error handling. It represents a value that is either a successful result or an error.
If the operation succeeds, it will contain a value of this type. If it fails, it will contain an error of this type. You can construct an expected object from a value of its value type, which is why this is allowed. You cannot construct an expected object from a value of its error type, however, which is why you need the unexpected helper.
You can invoke the value method on an expected object to access its value.
However, if it's holding an error, this will be undefined behavior. For this reason, you should check that your expected object has a value beforehand.
You can also dereference your expected object to access its value.
Or you can call value or, which will return its value if it has one, or return the value passed in if it doesn't. Expected has several monadic operations, of which transform is the simplest. It applies a function to the success value if it has one. As we can see, transform takes in a lambda, which takes in a parameter of the same type as our expected uh success value type. The transformation function can return a value of any type. This is the type returned by our invocation of transform.
To demo more monadic operations, I changed our transformation function to return an int and it just performed a squaring operation. And then is another monadic operation, which is used when the next step in the chain can itself fail. And then takes in our doubled input and checks that it can be doubled again without overflowing. And if it can't, it returns an error. Or else is the counterpart to and then, which helps you handle the error case. Similarly, the lambda passed into or else must return an expected. Transform and and then only run if the expected has a value. Or else only runs if the expected has an error. Let's demo our code now.
If this were production code, we may run into issues because squaring an integer can in fact overflow. So, we'd want to replace uh transform with uh and then.
But, um because I wanted to go over all the monadic operations, I kept it here.
Related Videos

TOP 15 Data compression Interview Questions and Answers 2019 Part-2 | Data compression | Wisdom jobs
wisdomjobs
281 views•2019-06-28

CTS 158: 802.11w Management Frame Protection
ClearToSend
4K views•2019-02-04

NDSS 2019 Send Hardest Problems My Way: Probabilistic Path Prioritization for Hybrid Fuzzing
NDSSSymposium
496 views•2019-04-02

How realistic is Cities: Skylines?
CityBeautiful
159K views•2019-02-14

GUIs & TUIs: Choosing a User Interface for Your Python Project | Real Python Podcast
realpython
2K views•2025-04-04

The OSI Model - Explained by Example
hnasr
225K views•2019-05-12

Cloud Computing - Introduction
elithecomputerguy
98K views•2019-10-07

From Traveler's Dilemma to Dynamic Routing | Demystifying Networking
IITBombayJuly
5K views•2019-08-04
Trending

One Must Imagine Sisyphus Happy
vlogbrothers
61K views•2026-07-21

Future of Taylor Farms
maighstirtarot5385
11K views•2026-07-21

The Downfall of OnePlus!
techwiser
65K views•2026-07-21

My Friend Locked Up The Engine On His K-Swapped Bug...
boostedboiz
128K views•2026-07-21