我试图在我的模型和控制器中使用异常。在application
目录中,我创建了一个目录Exceptions
,它在文件'CustomException1.php'和'CustomException2.php'中有一些异常类。在控制器中包含自定义异常文件
我application/core
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
// include Exception files
require_once(APPPATH . 'Exceptions/CustomException1.php');
require_once(APPPATH . 'Exceptions/CustomException2.php');
}
}
定义MY_Controller
在我的控制器Test
:
class Test extends MY_Controller {
public function index() {
try {
throw new CustomException1('This is a custom exception');
} catch (CustomException1 $e) {
$this->output->set_status_header('500');
echo $e->message;
}
}
现在,我希望这个工作,因为我要求所有定义异常类的文件,但我仍然得到一个错误说明
Class CustomException1 not found on line xx
啊,这将是我发布问题时发生的错误。我重命名了我抛给'CustomException'的实际异常。我现在会解决这个问题,但是谢谢你指出。 – xbonez