提供給還沒做過例外鏈接的人參考,以下是一個範例。
這允許您將先前的例外添加到下一個例外,並在最後提供發生的詳細資訊。這在較大型的應用程式中很有用。
<?php
function theDatabaseObj(){
if( database_object ){
return database_object;
}
else{
throw new DatabaseException("無法連線到資料庫");
}
}
function updateProfile( $userInfo ){
try{
$db = theDatabaseObj();
$db->updateProfile();
}
catch( DatabaseException $e ){
$message = "使用者 :" . $userInfo->username . " 無法更新他的個人資料";
throw new MemberSettingsException($message,12,$e);
}
}
try{
updateProfile( $userInfo );
}
catch( MemberSettingsException $e ){
echo $e->getTraceAsString();
}
?>