Book Image

Boost.Asio C++ Network Programming

By : Wisnu Anggoro
Book Image

Boost.Asio C++ Network Programming

By: Wisnu Anggoro

Overview of this book

Table of Contents (15 chapters)
Boost.Asio C++ Network Programming Second Edition
Credits
About the Authors
Acknowledgements
About the Reviewers
www.PacktPub.com
Preface
Index

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 */
#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/bind.hpp>
#include <iostream>

boost::mutex global_stream_lock;

void WorkerThread...