Skip to content

Instantly share code, notes, and snippets.

@gaffling
Last active February 7, 2020 11:36
Show Gist options
  • Save gaffling/5d1f56967d6d71fc1513879954a64073 to your computer and use it in GitHub Desktop.
Save gaffling/5d1f56967d6d71fc1513879954a64073 to your computer and use it in GitHub Desktop.
[Exeption] Custom Exceptions #php #class #errorhandling
<?php
/* ------------------------------------------------------- */
/* [Exeption] Custom Exceptions #php #class #errorhandling */
/* ------------------------------------------------------- */
interface IException {
/* Protected methods inherited from Exception class */
public function getMessage(); // Exception message
public function getCode(); // User-defined Exception code
public function getFile(); // Source filename
public function getLine(); // Source line
public function getTrace(); // An array of the backtrace()
public function getTraceAsString(); // Formated string of trace
/* Overrideable methods inherited from Exception class */
public function __toString(); // formatted string for display
public function __construct($message = null, $code = 0);
}
class CustomException extends Exception implements IException {
protected $message = 'Unknown exception'; // Exception message
private $string; // Unknown
protected $code = 0; // User-defined exception code
protected $file; // Source filename of exception
protected $line; // Source line of exception
private $trace; // Unknown
public function __construct($message = null, $code = 0) {
if (!$message) {
throw new $this('Unknown exception '. get_class($this));
}
parent::__construct($message, $code);
}
public function __toString(){
die( get_class($this) . $this->message .
' in ' . $this->file . '(' . $this->line . ')' . PHP_EOL .
$this->getTraceAsString()
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment