mardi 3 avril 2018

What is the proper way to raise exception and handle certain exception type in C++

I know theoretically how to create and handle exception. But in a big project a function can throw a lot of different exception.

How do I manage to parse all the exceptions I can receive?

For example:

I am currently implementing a database client. And if a query ask for a bad table or a bad column I throw exception in the low level driver as follow:

throw MySQLException("message");

And this will result in throwing a std::runtime_error exception in the client's top level. So I added the following code to catch the exception:

try {
  execute(query);
}
catch(std::exception &e) {
  // How do I parse exception content?
}

But is there a better way than excrating info from the e.what() to figure out what kind of exception it is? The objective is to make each sort of exception correspond to an error code (without modifiying the exception message).

Link to tutorial will be weclome.

Aucun commentaire:

Enregistrer un commentaire