00001 #include "FWCore/Utilities/interface/ExceptionCollector.h" 00002 #include "FWCore/Utilities/interface/Exception.h" 00003 #include "FWCore/Utilities/interface/ConvertException.h" 00004 00005 #include <exception> 00006 00007 namespace edm { 00008 00009 ExceptionCollector::ExceptionCollector(std::string const& initialMessage) : 00010 initialMessage_(initialMessage), 00011 firstException_(), 00012 accumulatedExceptions_(), 00013 nExceptions_(0) { 00014 } 00015 00016 ExceptionCollector::~ExceptionCollector() { 00017 } 00018 00019 bool 00020 ExceptionCollector::hasThrown() const { 00021 return nExceptions_ > 0; 00022 } 00023 00024 void 00025 ExceptionCollector::rethrow() const { 00026 if (nExceptions_ == 1) { 00027 firstException_->raise(); 00028 } 00029 else if (nExceptions_ > 1) { 00030 accumulatedExceptions_->raise(); 00031 } 00032 } 00033 00034 void 00035 ExceptionCollector::call(boost::function<void(void)> f) { 00036 try { 00037 try { 00038 f(); 00039 } 00040 catch (cms::Exception& e) { throw; } 00041 catch (std::bad_alloc& bda) { convertException::badAllocToEDM(); } 00042 catch (std::exception& e) { convertException::stdToEDM(e); } 00043 catch (std::string& s) { convertException::stringToEDM(s); } 00044 catch (char const* c) { convertException::charPtrToEDM(c); } 00045 catch (...) { convertException::unknownToEDM(); } 00046 } 00047 catch (cms::Exception const& ex) { 00048 ++nExceptions_; 00049 if (nExceptions_ == 1) { 00050 firstException_.reset(ex.clone()); 00051 accumulatedExceptions_.reset(new cms::Exception("MultipleExceptions", initialMessage_)); 00052 } 00053 *accumulatedExceptions_ << nExceptions_ << "\n" 00054 << ex.explainSelf(); 00055 } 00056 } 00057 00058 void 00059 ExceptionCollector::addException(cms::Exception const& exception) { 00060 ++nExceptions_; 00061 if (nExceptions_ == 1) { 00062 firstException_.reset(exception.clone()); 00063 accumulatedExceptions_.reset(new cms::Exception("MultipleExceptions", initialMessage_)); 00064 } 00065 *accumulatedExceptions_ << "----- Exception " << nExceptions_ << " -----" 00066 << "\n" 00067 << exception.explainSelf(); 00068 } 00069 }