Kohana之异常编写

在app~/class~/下建立exception目录(统一放这里比较好管理)

建立一个test.php 文件

内容为:

<?php defined('SYSPATH') or die('No direct script access.');

class Exception_Test extends Kohana_Exception {
	
}


内容可以根据自己的需要定制

使用:

建立一个模型:

<?php defined('SYSPATH') or die('No direct script access.');
class Model_Test extends Model{
	public function __construct($db=null){
		parent::__construct($db);
	}
	public function test(){
		throw new Exception_Test("test model exp in class :class",array(":class"=>get_class($this)));
	}
}


假设在控制器中使用:

<?php
$test=Model::factory("test");
try{
	$test->test();
}catch(Exception_Test $e){
	echo $e->getMessage();
}

一个异常的使用的完整示例,根据自己的需要去扩展吧~

原文地址:https://www.cnblogs.com/liushannet/p/1806270.html