I'm currently working with PHP and have been doing in and else inside a try and catch block.
it looks like this
try {
if (!Class::classMethod()) {
throw new Exception("error!");
}
if (!Class::anotherClassMethod()) {
throw new Exception("another type of error!");
}
} catch (Exception $e) {
// send error message to index.php as a readable error for users
}
and class method does some code, returns true or a value on success, returns false on failure.
I have been experimenting with exceptions and thought of doing this instead:
Class.php
class Class {
function method() {
// executing some code
if (failed) {
throw new Exception("error!");
}
return;
}
and then :
index.php
require_once("Class.php");
try {
Class::method();
} catch (Exception $e) {
// reload index.php with error message as a readable error for users
}
Which one is recommended and why? thank you!
Aucun commentaire:
Enregistrer un commentaire