Usually what I do is wrap potential problematic code - such as database connections - with try/catch blocks. Then if any errors are caught, put human readable equivalants in a global array so you can safely output the list to the template.
For instance:
<?php
$errors = array();
try
{
if (!$var)
throw new Exception('Meh..');
}
catch (Exception $e)
{
// Use $e with caution, as with database connection it can sometimes contain your password. :P
$errors[] = 'Something is seriously wrong with that last statement.<br />' . $e;
}
// Template layers..
echo '
<ul>';
foreach ($errors as $error)
echo '
<li>', $error, '</li>';
echo '
</ul>';