2014-11-02 34 views
1

是否有PHP任何方式來分割在兩個不同的trycatch「包含文件」,將在一個文件中try和另一個catch拆分嘗試和catch異常包括文件

喜歡的東西:

<?php 
    include('begin_try.php'); 
     ... 
     //some code goes here 
     ... 
    include('end_try_and_catch_exceptions.php'); 
?> 

//文件名:begin_try.php:

<?php 
    header('Content-type: application/json'); 
    try { 
?> 

//文件名:end_try_and_catch_exceptions.php:

<?php 
    } catch (Exception $e) { 
     echo json_encode(array(
      'error' => array(
       'code' => $e->getCode(), 
       'message' => $e->getMessage() 
     ) 
    )); 
    } 
?> 

回答

1

這不是語法可以使用嘗試捕捉那樣,但你可以使用set_exception_handler函數來註冊一個函數將捕獲所有未被捕獲的異常:

set_exception_handler(function ($e) { 
    echo json_encode(array(
     'error' => array(
      'code' => $e->getCode(), 
      'message' => $e->getMessage() 
     ) 
    )); 
}); 
+0

由於沒有人提供更好的想法,我會將您的答案標記爲正確答案。 – Kainax 2015-07-12 07:49:25