OExceptions.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. interface IException
  3. {
  4. /* Protected methods inherited from Exception class */
  5. public function getMessage(); // Exception message
  6. public function getCode(); // User-defined Exception code
  7. public function getFile(); // Source filename
  8. public function getLine(); // Source line
  9. public function getTrace(); // An array of the backtrace()
  10. public function getTraceAsString(); // Formated string of trace
  11. /* Overrideable methods inherited from Exception class */
  12. public function __toString(); // formated string for display
  13. public function __construct($message = null, $code = 0);
  14. }
  15. abstract class CustomException extends Exception implements IException
  16. {
  17. private $trace; // Unknown
  18. private $string; // Unknown
  19. protected $message = 'Unknown exception'; // Exception message
  20. protected $code = 0; // User-defined exception code
  21. protected $file; // Source filename of exception
  22. protected $line; // Source line of exception
  23. public function __construct($message = null, $code = 0)
  24. {
  25. if (!$message) {
  26. throw new $this('Unknown '. get_class($this));
  27. }
  28. parent::__construct("Err: ".$message."\n", $code);
  29. }
  30. public function __toString() {
  31. $err=$this->message;
  32. if($this->code!=0)
  33. $err= get_class($this).
  34. " '{$this->message}' in {$this->file}({$this->line})\n".
  35. "{$this->getTraceAsString()}";
  36. return $err;
  37. }
  38. }
  39. class DbErrException extends CustomException{};
  40. class DbResException extends DbErrException{};
  41. class UsageException extends CustomException{
  42. public function __construct($message = null, $code = 0)
  43. {
  44. if (!$message and $code == 0) {
  45. throw new $this('Unknown '. get_class($this));
  46. } elseif ($message != null and $code > 0) {
  47. $argv="Err: ".$message."\n";
  48. $message="Bad arg: ".$argv." is not a valid command, with ".$code." arguments\n";
  49. $code=0;
  50. }
  51. parent::__construct($message, $code);
  52. }
  53. public function __toString() {
  54. return $this->message;
  55. }
  56. }
  57. class OtConfException extends CustomException{};
  58. class FileException extends CustomException{
  59. public function __toString() {
  60. return $this->message;
  61. }
  62. };
  63. ?>