Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:29:22

0001 
0002 #include "FWCore/Utilities/interface/EDMException.h"
0003 
0004 #include <iostream>
0005 #include <string>
0006 #include <iomanip>
0007 #include <typeinfo>
0008 #include <memory>
0009 
0010 void func3() {
0011   edm::Exception ex(edm::errors::NotFound);
0012   ex << "This is just a test";
0013   ex.addContext("new1");
0014   ex.addAdditionalInfo("info1");
0015   if (ex.returnCode() != 8026) {
0016     abort();
0017   }
0018   if (ex.category() != std::string("NotFound")) {
0019     abort();
0020   }
0021   throw ex;
0022 }
0023 
0024 void func2() { func3(); }
0025 
0026 void func1() {
0027   try {
0028     func2();
0029   } catch (edm::Exception& e) {
0030     edm::Exception toThrow(edm::errors::Unknown, "In func2", e);
0031     edm::Exception toThrowString(edm::errors::Unknown, std::string("In func2"), e);
0032     if (toThrow.explainSelf() != toThrowString.explainSelf()) {
0033       abort();
0034     }
0035     toThrow << "\nGave up";
0036     toThrow.addContext("new2");
0037     toThrow.addAdditionalInfo("info2");
0038     if (toThrow.returnCode() != 8003) {
0039       abort();
0040     }
0041     if (toThrow.categoryCode() != edm::errors::Unknown) {
0042       abort();
0043     }
0044     cms::Exception* ptr = &toThrow;
0045     ptr->raise();
0046   }
0047 }
0048 
0049 const char answer[] =
0050     "An exception of category 'Unknown' occurred while\n"
0051     "   [0] new2\n"
0052     "   [1] new1\n"
0053     "Exception Message:\n"
0054     "In func2\n"
0055     "This is just a test\n"
0056     "Gave up\n"
0057     "   Additional Info:\n"
0058     "      [a] info2\n"
0059     "      [b] info1\n";
0060 
0061 int main() {
0062   try {
0063     func1();
0064   } catch (edm::Exception& e) {
0065     if (e.explainSelf() != answer) {
0066       std::cerr << "Exception message incorrect.\n"
0067                    "==expected==\n"
0068                 << answer << "\n==message==\n"
0069                 << e.explainSelf() << std::endl;
0070       abort();
0071     }
0072     edm::Exception ecopy(e);
0073     if (e.explainSelf() != ecopy.explainSelf()) {
0074       abort();
0075     }
0076   } catch (cms::Exception& e) {
0077     abort();
0078   }
0079 
0080   edm::Exception e1(edm::errors::Unknown, "blah");
0081   edm::Exception e1String(edm::errors::Unknown, std::string("blah"));
0082   if (e1.explainSelf() != e1String.explainSelf()) {
0083     abort();
0084   }
0085   if (e1.returnCode() != 8003) {
0086     abort();
0087   }
0088   if (e1.category() != std::string("Unknown")) {
0089     abort();
0090   }
0091   if (e1.message() != std::string("blah ")) {
0092     abort();
0093   }
0094   cms::Exception* ptr = &e1;
0095   cms::Exception* ptrCloneCopy = ptr->clone();
0096   if (ptrCloneCopy->returnCode() != 8003) {
0097     abort();
0098   }
0099   try {
0100     edm::Exception::throwThis(edm::errors::ProductNotFound, "a", "b", "c", "d", "e");
0101   } catch (edm::Exception& ex) {
0102     if (ex.explainSelf() != std::string("An exception of category 'ProductNotFound' occurred.\n"
0103                                         "Exception Message:\n"
0104                                         "a bcde\n")) {
0105       abort();
0106     }
0107   }
0108 
0109   try {
0110     edm::Exception::throwThis(edm::errors::ProductNotFound, "a", 1, "b");
0111   } catch (edm::Exception& ex) {
0112     if (ex.explainSelf() != std::string("An exception of category 'ProductNotFound' occurred.\n"
0113                                         "Exception Message:\n"
0114                                         "a 1b\n")) {
0115       abort();
0116     }
0117   }
0118 
0119   return 0;
0120 }