Posted by DD on Wed 20th Jun 12:14 (modification of post by DD view diff)
diff | download | new post
- #include <string>
- #include <exception>
- #include <sstream>
- class parse_error : public std::exception
- {
- public:
- parse_error(const std::string& file, size_t line, const std::string& message)
- {
- _message = (std::ostringstream()
- << "Error in file "
- << file
- << ", line "
- << line
- << ":\n"
- << message
- ).str();
- }
- virtual ~parse_error() { }
- virtual const char * what() const throw() { return _message.c_str(); }
- private:
- std::string _message;
- };
- thrower_function()
- {
- throw parse_error("some_file.txt", 42, "Meh.");
- }
- catcher_function()
- {
- try {
- do_something();
- thrower_function();
- do_some_other_stuff();
- } catch (parse_error &e) {
- std::cerr << e.what() << std::endl;
- }
- }
Submit a correction or amendment below. (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.