I'm currently implementing PHP 5 exceptions in MediaWiki and I've come
across a design question. How often should exceptions be used?
I've set up an exception handler which is quite capable of formatting
and displaying pretty error messages for the user. The HTML to be
displayed is defined by the exception class. But the question is, how
often should I use them? Say if I have the choice between:
function deleteUserInterface( $file ) {
if ( deleteFile( $file ) ) {
displaySuccess();
} else {
displayError();
}
}
function deleteFile( $file ) {
return (bool) unlink( $file );
}
function displayError() {
// pretty error
}
and...
function deleteUserInterface( $file ) {
deleteFile( $file );
displaySuccess();
}
function deleteFile( $file ) {
if ( !unlink( $file ) ) {
throw new DeleteError();
}
}
class DeleteError extends MWException{
function getHTML() {
// pretty error
}
}
which is better? Should the backend dictate the error format by
specifying an exception class with a fixed format? Of course, a caller
wishing to override the formatting could catch the exception, but is
that better than the old method of using a success/failure return value?
Should error handling be completely exception-dominated? Is there any
role for success/failure return values in a language with exception support?
-- Tim Starling