Handling exceptions and errors

Sometimes, our code will throw an exception or error at runtime. As you may remember in our discussion of the lexical.cpp in Chapter 3, Introducing the Boost C++ Libraries, we must sometimes use exception handling in our code, and we will now dig it up to delve into exception and error handling.

Handling an exception

An exception is a way of reacting to a situation in which the code has exceptional circumstances by transferring control to the handler. To handle the exception, we need to use the try-catch block in our code; then, if an exceptional circumstance arises, an exception will be thrown to the exception handler.

Now, take a look at the following code to see how exception handling is used:

/* exception.cpp */ ...

Get Boost.Asio C++ Network Programming - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.