Hi,


It’s a minor point in the discussion, maybe, but Meyers’ ‘More Effective
C++’ recommends always catching exceptions by reference.  It’s quicker than
catching by value because it avoids copy construction, and, more
importantly, it avoids the problem of class slicing if a derived class
exception object is caught as a base exception.



Paul’s code snippet becomes



try {
   rwmol = SmilesToMol(smiles_strings[i]);
   // cool stuff here...
}
catch (RDKit::MolSanitizeException &msg) {
    // do something (or nothing)...
   std::cout << msg.what() << std::endl;
}

If you were to write



try {
   rwmol = SmilesToMol(smiles_strings[i]);
   // cool stuff here...
}
catch ( std::exception msg) {
    // do something (or nothing)...
   std::cout << msg.what() << std::endl;
}

you would get std::exception::what() not RDKit::MolSanitizeException::what()
because copying the RDKit exception to the std one has sliced away the
extra.  Passing by reference retains the virtual call to the expected
RDKitobject. You should also note that whatever is pointed to by rwmol
is
undefined.  Throwing an exception is not just another way of passing a
return value. After the try/catch block, it will be as if SmilesToMol was
never called in the first place.  Also, Meyers recommends only using try
blocks when your really need to, as they add an overhead to both code size
and runtime.  If you want your code to be as fast as possible, and the
thing just blowing up with a strange looking error message about exceptions
is an adequate error message for you, then don't bother.


Dave
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Rdkit-discuss mailing list
Rdkit-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rdkit-discuss

Reply via email to