日期:2014-05-17  浏览次数:20376 次

try catch throw php搜集

我个人的理解是:?
1。在private或者protected的成员函数不使用try,catch,而只使用throw?
2。如果在private或者protected的成员函数需要使用try,catch,那么就要使用rethrow?
3。在public成员函数里使用try,catch?
4。如果该类相对于整个项目来说是属于被调用层,那么public成员函数也可以不使用try,catch?
5。如果调用第三方的代码,我一般都会用try,catch?
class CTest1;?
class CTest2;?
class CTest3;?

void BadCode()?
{?
? //define?
? CTest1 * pTest1 = NULL;?
? CTest2 * pTest2 = NULL;?
? CTest3 * pTest3 = NULL;?

? //使用try, catch, throw?
? try?
? {?
? ? //new test1?
? ? pTest1 = new CTest1;?

? ? //do something?
? ? bool bRet = DoSomething();?
? ? if (!bRet)?
? ? ? throw -1;?

? ? //new CTest2?
? ? pTest2 = new CTest2;?

? ? //do something?
? ? bRet = DoSomething();?
? ? if (!bRet)?
? ? ? throw -2;?

? ? //new CTest3?
? ? pTest3 = new CTest3;?

? ? bRet = DoSomething();?
? ? //do something?
? ? if (!bRet)?
? ? ? throw -3;?

? ? //release?
? ? delete pTest1;?
? ? pTest1 = NULL;?
? ? delete pTest2;?
? ? pTest2 = NULL;?
? ? delete pTest3;?
? ? pTest3 = NULL;?
? }?
? catch(...)?
? {?
? ? if (pTest1)?
? ? ? delete pTest1;?
? ? if (pTest2)?
? ? ? delete pTest2;?
? ? if (pTest3)?
? ? ? delete pTest3;?
? }?
}

//-----------------------------------------------------------------------

try
{
..........
.........
//throw
}
catch (int