如何禁用PHP的堆栈跟踪?

My default PHP configuration prints a stack trace in the browser window. How can I disable it and replace it with a custom error page?

You can run exception handlers to modify the behaviour of the exception and replace stack traces.

For example:

<?php
function exception_handler($exception) {
   echo "Uncaught exception: " , $exception->getMessage(), "
";
  }

   set_exception_handler('exception_handler');

   throw new Exception('Uncaught Exception');
   echo "Not Executed
";
  ?>

This can be modified to run specific instructions when encountering an error.

An exception handler handles exceptions that were not caught before. It is the nature of an exception that it discontinues execution of your program - since it declares an exceptional situation in which the program cannot continue (except you catch it)

For cases like this, you can run it in a class

If you want a class instance to handle the exception, this is how you do it :

<?php
class example {
   public function __construct() {
   @set_exception_handler(array($this, 'exception_handler'));
   throw new Exception('DOH!!');
   }

    public function exception_handler($exception) {
       print "Exception Caught: ". $exception->getMessage() ."
";
    }
 }

     $example = new example;

   ?>