📄 texcept.cpp
字号:
// texcept.cc// test exceptions// copyright SafeTP Development Group, Inc., 2000 Terms of use are as specified in license.txt#include "exc.h" // exceptions#include "datablok.h" // DataBlock#include <stdio.h> // printf // count errorsint errors = 0;class C { int seven;public: C(int throwIt); ~C(); void foo() {}};C::C(int throwIt) : seven(7){ if (throwIt) { THROW(xBase("thrown from ctor")); }}C::~C(){ printf("in C::~C(), seven = %d\n", seven);}class D { C c;public: D();};D::D() : c(0 /*throwit*/){ THROW(xBase("thrown from D's dtor"));}void throwit(){ THROW(xBase("thrown from throwit()"));}void throwOne(char const *msg){ THROW(xBase(msg));}class Thrower {public: virtual void th()=0;};class AThrower : public Thrower {public: virtual void th();};void AThrower::th(){ C c(0 /*throwit*/); c.foo(); THROW(xBase("thrown from AThrower::th"));}// branded public keyclass BPK { DataBlock bk; C c;public: BPK(DataBlock const &k, Thrower &t);};BPK::BPK(DataBlock const &k, Thrower &t) : bk(k), c(0 /*throwit*/){ //C c2(1 /*throwit*/); //c2.foo(); //throwit(); t.th();}// something derived from xBaseclass xDeriv : public xBase {public: int y; xDeriv() : xBase("xDeriv"), y(4) {} xDeriv(xDeriv const &x) : xBase(x), y(x.y) {}};class SF { // SFTPCpublic: void ck150(); void sdt(); void dt(); void mfc(); void di();};void SF::ck150() // check150Reply{ xDeriv x; printf("throwing xDeriv\n"); THROW(x);}void SF::sdt() // startDataTransfer{ ck150();}void SF::dt() // dataTransfer{ try { sdt(); } catch (...) { printf("caught ... in dt\n"); throw; // here is where I suspect the problem is }}int someVal = 1;void SF::mfc() // multipleFileCommand{ for(;;) { try { if (someVal > 0) { dt(); } else { throwOne("foo"); } } catch (xBase &x) { printf("ok: caught xBase in mfc\n"); } catch (...) { errors++; printf("WTF?!\n"); throw; } break; }}void SF::di() // doit{ try { mfc(); } catch (xDeriv &x) { errors++; printf("FAIL: caught xDeriv in di\n"); } catch (xBase &x) { errors++; printf("FAIL: caught xBase in di\n"); } catch (...) { errors++; printf("FAIL: caught ... in di\n"); }}int main(){ printf("first one\n"); try { // was throwing "yadda" here but compilers differ so much // on char* (signed/unsigned, const/non-const, ..) I've // switched the first test to ints which are much less // vulnerable to bizarre interpretations throw 1; } catch (int x) { printf("caught int: %d\n", x); } catch (...) { errors++; printf("FAIL: caught something unknown\n"); } printf("\nsecond one\n"); try { THROW(xBase("yadda")); errors++; printf("FAIL: shouldn't get here\n"); } catch (xBase &x) { printf("caught: %s\n", x.why()); } catch (...) { errors++; printf("FAIL: caught something unknown\n"); } printf("\ntesting destructors during unwind\n"); try { C c(0 /*throwIt*/); c.foo(); THROW(xBase("should cause dtor")); } catch (xBase &x) { printf("caught: %s\n", x.why()); } try { C c(1 /*throwIt*/); c.foo(); } catch (xBase &x) { printf("caught: %s\n", x.why()); } try { D d; } catch (xBase &x) { printf("caught: %s\n", x.why()); } printf("\ntrying to recreate bpk segfault\n"); try { AThrower t; DataBlock blk("yadda smacker"); BPK bpk(blk, t); } catch (xBase &x) { printf("caught: %s\n", x.why()); } printf("\ndo exceptions thrown during handler work?\n"); try { C c(0); throwOne("first thrown"); } catch (xBase &x) { printf("caught the first, throwing one inside the handler\n"); try { C c(0); throwOne("second thrown"); } catch (xBase &x) { printf("caught the second ok\n"); } } printf("\n"); SF sf; sf.di(); printf("\nend of tests : %d errors\n", errors); return errors;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -