I'm developing for a project where HTML designers use PHP methods to retrieve content. Now, it's the time for handling exceptions and we are running into problems to simultaneously keep the HTML pretty, clean and semantic and the PHP correct.
Until exceptions, HTML templates had things like:
<h2 class="title"><?php Content::getTitle() ?></h2>
Now, if we want to handle exceptions, we are supposed to write:
<h2 class="title">
<?php
try {
Content::getTitle();
} catch (Exception $e) {
echo $e->getMessage();
}
?>
</h2>
Which is anything but clean.
Or
<?php
try {
// THE WHOLE TEMPLATE RENDERING!!!
} catch (Exception $e) {
echo $e->getMessage();
}
?>
Which is unacceptable because it stops rendering at any exception, when it should report the problem and continue.
So, we are thinking about putting the try/catch stuff inside the method itself:
class Content {
public static function getTitle {
try {
if (something==happened) throw new Exception 'OMG!';
else {
DoTheJob();
}
} catch (Exception $e) {
echo $e->getMessage();
}
}
It seems to work by the moment so the question is: is this a good practice? Is there a better and more DRY alternative?