005
04.02.2004, 11:40 Uhr
virtual
Sexiest Bit alive (Operator)
|
Ich finde, man kann das schon sinnvoll einsetzen, ohne daß man in der funktion selbst was wirft. Siehe exception_logger
| C++: |
#include <iostream> #include <stdexcept>
#define LOG_EXCEPTION(e) exception_logger(__FILE__, __LINE__, (e));
void exception_logger(const char* p_szFilename, unsigned p_nLine, std::exception* p_poException) { std::cerr<<"Exception caught at "<<p_szFilename<<"("<<p_nLine<<")"; // Ausgabe der Exception nur, wenn von std::exception abgelitten if (NULL != p_poException) { std::cerr<<": "<<p_poException->what(); } std::cerr<<std::endl; // Exception aber jedenfalls rethrown throw; }
int teile(int a, int b) { try { if (b==0) throw std::runtime_error("division_by_zero"); return a/b; } catch(std::exception& e) { LOG_EXCEPTION(&e); } catch(...) { LOG_EXCEPTION(NULL); } }
int dreisatz(int a, int b, int c) { try { return c*teile(a,b); } catch(std::exception& e) { LOG_EXCEPTION(&e); } catch(...) { LOG_EXCEPTION(NULL); } }
int main() { try { std::cerr<<"4/0*5="<<dreisatz(4,0,5); } catch(...) { std::cerr<<"Something failed. See output where."<<std::endl; } }
|
-- Gruß, virtual Quote of the Month Ich eß' nur was ein Gesicht hat (Creme 21) |