I have recently been looking into exceptions and setting up a more comprehansive error catching/reporting system that will notify me of any errors that happen within my systems.
The literature i have been reading always involves using try on every bit of code which is written then throwing an exception if an error occurs which is then caught by another statement performing the error reporting functionality.
If there a way without having to do something along these lines:
class FileReader{
private $file;
private $fileDir='fileDir/';
public function __construct($file){
if(!file_exists("{$this->fileDir}{$file}.php")){
throw new FileException('File '.$file.' not found');
}
$this->file=$file;
}
public function getContent(){
if(!$content=file_get_contents("{$this->fileDir}{$this-
file}.php")){
throw new FileException('Unable to read file contents');
}
return $content;
}
public function mailContent(){
if(!mail('Recipient<user@domain.com>','HTML Page',$this-
getContent())){
throw new MailException('Unable to send by email file
contents');
}
}
}
try{
$fr=new FileReader('sample_file');
// email file content
$fr->mailContent(); // potential error condition
// echo file content
echo $fr->getContent(); // potential error condition
}
// catch FileException
catch(FileException $e){
$e->showError();
}
// catch MailException
catch(MailException $e){
$e->logError();
}
I was thinking something more along the lines of throwing exceptions on each error occurance then catching/performing the reporting within the destruct of each class.
I don't know how viable this is. also does anyone have any decent references/uses of exceptions within a system.
thanks in advance.