PHP Kohana异常处理

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP Kohana异常处理相关的知识,希望对你有一定的参考价值。

function hideme($auth=''){
	//try something first
	try{
			//if not TRUE throw an exception with error message
			if(empty($auth)) throw new Kohana_User_Exception('Parameter Empty','You should include a value');		
			//following lines won't work if an exception occur
			//another method where an exception may occur
			$this->checkNum($auth);
			//the last line of try catch block and if you see this text. Error FREE :)
			echo 'You passed a number :: '.$auth;
		//catch specific kohana_user_exception because it inheritance from kohana_exception	
		}catch(kohana_user_exception $e){
			//call a method before die:: very useful for closing </html> tags or closing DB
			die($this->error_handler($e));
		//not necessary in this sample code but if above exceptions fail to catch..
		//at least parent exception should be called like this
		}catch(kohana_exception $e){
			die($e->getMessage());
		}
	}
	//to be called before die
	private function error_handler($e){
		echo 'do something before die!<br />';
		echo $e;
	}
	
	function checkNum($num){
		if(!(int)$num > 0) throw new Kohana_User_Exception('Invalid Number','This value is not a number. Please provide a valid number.');
		return TRUE;
	}

以上是关于PHP Kohana异常处理的主要内容,如果未能解决你的问题,请参考以下文章

如何正确设置 Kohana 的 .htaccess,使 URL 中没有丑陋的“index.php/”?

Kohana 3,添加 m2m 数据的“未定义索引”异常

搜索 Kohana 初学者的 PHP 教程 [关闭]

Kohana3 ORM 需要澄清关系

PHP Kohana决赛课

PHP 获取当前在Kohana 3的路线