6.2 The XStrom exception class

(Linux version)

< 6.1 | 6.2 | 6.3 >

Unexpected things happen while a program is running. Users may not use the program in the way intended by the developer, and assumptions made by the developer that are violated by the user should result in an informative message rather than a non-informative crash. The way this is handled in C++ is through exceptions. An exception object is “thrown” when the program realizes that an assumption has been violated, and the object is “caught” at another place in the program where it can be reported to the user. Look for the try block inside the buildFromNewick function. If anything bad happens inside the try block, an exception is thrown and the exception object is caught in the catch block near the bottom of the buildFromNewick function. Note what happens when the exception is caught: the clear function is called to ensure that the Tree object is not left in a partially-built state, then the exception is thrown again. We will experiment with catching the second throw in the main function and reporting the problem there, but for now we need to create the XStrom exception class.

Create a new class named XStrom in a file named xstrom.hpp and fill the file with the following code:

#pragma once    

#include &lt;boost/format.hpp&gt;

namespace strom {

    class XStrom : public std::exception {
        public:
                                XStrom() throw() {}
                                XStrom(const std::string s) throw() : _msg() {_msg = s;}
                                XStrom(const boost::format & f) throw() : _msg() {_msg = boost::str(f);}
            virtual             ~XStrom() throw() {}
            const char *        what() const throw() {return _msg.c_str();}

        private:

            std::string         _msg;
    };

}   

You should have already done this, but check now that you have added the following line to the top of tree_manip.hpp so that the compiler will know about our new XStrom class:

#include "xstrom.hpp"

< 6.1 | 6.2 | 6.3 >