Kohana Exception Handling


/ Published in: PHP
Save to your folder(s)

Some working example code snippet..


Copy this code and paste it in your HTML
  1. function hideme($auth=''){
  2. //try something first
  3. try{
  4. //if not TRUE throw an exception with error message
  5. if(empty($auth)) throw new Kohana_User_Exception('Parameter Empty','You should include a value');
  6. //following lines won't work if an exception occur
  7. //another method where an exception may occur
  8. $this->checkNum($auth);
  9. //the last line of try catch block and if you see this text. Error FREE :)
  10. echo 'You passed a number :: '.$auth;
  11. //catch specific kohana_user_exception because it inheritance from kohana_exception
  12. }catch(kohana_user_exception $e){
  13. //call a method before die:: very useful for closing </html> tags or closing DB
  14. die($this->error_handler($e));
  15. //not necessary in this sample code but if above exceptions fail to catch..
  16. //at least parent exception should be called like this
  17. }catch(kohana_exception $e){
  18. die($e->getMessage());
  19. }
  20. }
  21. //to be called before die
  22. private function error_handler($e){
  23. echo 'do something before die!<br />';
  24. echo $e;
  25. }
  26.  
  27. function checkNum($num){
  28. if(!(int)$num > 0) throw new Kohana_User_Exception('Invalid Number','This value is not a number. Please provide a valid number.');
  29. return TRUE;
  30. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.