Return to Snippet

Revision: 26294
at April 22, 2010 15:12 by philokezzar


Updated Code
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;
	}

Revision: 26293
at April 22, 2010 15:00 by philokezzar


Initial Code
function hideme($auth=''){
     try{
	if(empty($auth)) throw new Kohana_User_Exception('Parameter Empty','You should include a value');		
			$this->checkNum($auth);
			echo 'You passed a number :: '.$auth;
		}catch(kohana_user_exception $e){
			die($this->error_handler($e));
		}catch(kohana_exception $e){
			die($e->getMessage());
		}
	}

	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;
	}

Initial URL


Initial Description
Some working example code snippet..

Initial Title
Kohana Exception Handling

Initial Tags


Initial Language
PHP