C++Now 2014 has ended
Please visit the C++Now website.
Back To Schedule
Friday, May 16 • 2:30pm - 4:00pm
Expected — An Exception-friendly Error Monad

Log in to save this to your schedule, view media, leave feedback and see who's attending!

The Expected library is based on the Expected class introduced by A. Alexandrescu in 2012. It is a new way to handle errors in C++, lying somewhere between the classic error-code returns and exceptions. Expected is fully compatible with exception-throwing code and helps to design exception-free interfaces. This open the door to novel techniques enforcing error handling safety while keeping code clean through the use of monads, which are borrowed from functional languages like Haskell. No prior knowledge of monads is needed, however.

The talk will start with a presentation of the existing techniques for handling errors. We will compare the return error code and the exception systems and then we will introduce expected in this context. The std::future and the std::experimental::optional classes are similar utility classes and we will speak about the conceptual differences among these classes.

The core of the Expected library will be presented next using an example to show the different features and semantics of the expected class. We will discuss how the expected class can be used to define exception-free interfaces using any user defined error type.

The monad error methods next and recover will be presented then through several examples. They enable programming techniques that help to distinguish the error-handling layer from the normal code flow. Moreover, they help the programmer to handle errors in a non-invasive way while being visible if forgotten. We will briefly talk about monads in functional languages and why expected can be seen as a monad.

The rest of the talk will discuss the possibility of using a similar interface for other potential monad classes such as std::experimental::optional and std::future.

As for Haskell monads, some syntactic sugar could make the code more readable. We will present how the use of a new expect expression, similar to the await expression presented in N3858 Resumable functions,  could make the code as readable as if the function could throw an exception.

Note: This library has not been proposed yet to the C++ standard, but should be by the time of the conference.

avatar for Vicente J. Botet Escriba

Vicente J. Botet Escriba

Vicente J. Botet Escriba got a Masters in Computer Science from University Complutense of Madrid, Spain in 1986. He has over 25 years of experience in software engineering for fault tolerant systems, and over 15 years of C++ experience. His main research area is on concurrent systems... Read More →

Friday May 16, 2014 2:30pm - 4:00pm MDT

Attendees (0)