试试Catch不工作? [重复]

This question already has an answer here:

I cannot make try work. I tried this:

try {
    echo 1/0;
    } catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "
";
    }

Which results on the error:

Warning: Division by zero in /var/www/vhosts/saliganando.com/webs/momemi/apis/interpret-bot.php on line 6 

I tried modifying error_reporting() and ini_set() but I have only managed to either remove the warning or display it, but 'Caught exception...' is never shown.

What am I doing wrong?

</div>

That code will never generate an exception. It generates a warning. You would need to capture the warning within an error handler (with set_error_handler()) to process that error.

See the docs on exceptions for plenty of examples on how Exceptions work and how to catch them, including one to mimic the functionality you're looking for:

function inverse($x) {
    if (!$x) {
        throw new Exception('Division by zero.');
    }
    else return 1/$x;
}

try {
    echo inverse(5) . "
";
    echo inverse(0) . "
";
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "
";
}

Which generates:

0.2
Caught exception: Division by zero.