Php 包括具有try-and-catch功能的文件

Php 包括具有try-and-catch功能的文件,php,Php,如果try函数中的include不可用,我将尝试测试catch函数以显示test.html文件。我做了测试,结果什么也没显示。我用错误的方式写了这段代码吗 谢谢你的智慧 您必须使用该函数来处理文件可用性,而不是尝试。。。捕捉块 另外请注意,对于包含web url,您可能需要设置以下allow\u url\u fopen指令。使用require而不是include,这是因为您注释掉了您的include吗?@DirkScholten说的,include可以正常工作include(文件存在('fi

如果try函数中的include不可用,我将尝试测试catch函数以显示test.html文件。我做了测试,结果什么也没显示。我用错误的方式写了这段代码吗


谢谢你的智慧

您必须使用该函数来处理文件可用性,而不是
尝试。。。捕捉



另外请注意,对于包含web url,您可能需要设置以下
allow\u url\u fopen
指令。

使用
require
而不是include,这是因为您注释掉了您的include吗?@DirkScholten说的,include可以正常工作
include(文件存在('file.php')?'file.php':'404.php')-只需一行即可;)@太棒了@我在问题中看到了你的评论。也许你可以将其作为替代建议或解决方案。OP代码没有任何错误,只是他们注释掉了导致错误的代码,因此没有错误,也没有异常可捕获。谷歌搜索和随机结果:
<?php
    function exception_error_handler($errno, $errstr, $errfile, $errline ) 
    {
       throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
    }
       set_error_handler("exception_error_handler");

   try {
      //include 'http://www.svrsstatus.com/banner.html';  
      //include 'http://www.svrsstatus.com/banner_outage_sample.html';  
   } 
   catch (ErrorException $ex) {
       /* echo "Unable to load configuration file.";  */
       include 'http://staging.sorenson.com/test.html';
   }  
?>