Using Custom Exceptions to Handle Different Circumstances
<?php
class MyException extends Exception {
public function summarize() {
$ret = "<pre>\n";
$ret .= "msg: ".$this->getMessage()."\n"
."code: ".$this->getCode()."\n"
."line: ".$this->getLine()."\n"
."file: ".$this->getFile()."\n";
$ret .= "</pre>\n";
return $ret;
}
}
class FileNotFoundException extends MyException { }
class FileOpenException extends MyException { }
class Reader {
function getContents( $file ) {
throw new FileNotFoundException( "could not find '$file'" );
throw new FileOpenException( "unable to open '$file'" );
}
}
$reader = new Reader();
try {
print $reader->getContents( "blah.txt" );
} catch ( FileNotFoundException $e ) {
print $e->summarize();
} catch ( FileOpenException $e ) {
print $e->summarize();
} catch ( Exception $e ) {
die("unknown error");
}
?>
Related examples in the same category