2

Curiously, I made a Class with the same interface like base Exception Class but not extending/ derived from it. Just old plain PHP object to act like an exception. I knew this would not work as explained here but tried anyway since after all exceptions are just like normal classes.

class MyException {

/**
* properties omitted
*/

 public function __construct( string $message = "",int $code)

 public function getMessage(void)

 //more methods
} 

// somewhere in code block

  throw new MyException("Fatal error");
  // Fatal error: exceptions must be valid objects derived from exception base class.

Can you please explain other ways exceptions are different from regular classes and how they achieve their roles of being exceptional in our code. I know this might be stupid question, but I was just hoping to understand more about how exceptions really work.

1
  • 1
    Only throwables can be thrown. Not any class. It's just a language rule. Commented Oct 9, 2017 at 7:58

2 Answers 2

2

Exception is normal class. It has to extend 'Exception', because PHP doesn't want to check if it has all required methods/properties (used by default PHP exception handler).

By extending class (or implementing interface) you prove that your class has methods like 'getMessage()' and it won't throw error about not existing method.

It must implement all Throwable interface methods (http://php.net/manual/en/class.throwable.php):

Throwable {
    /* Methods */
    abstract public string getMessage ( void )
    abstract public int getCode ( void )
    abstract public string getFile ( void )
    abstract public int getLine ( void )
    abstract public array getTrace ( void )
    abstract public string getTraceAsString ( void )
    abstract public Throwable getPrevious ( void )
    abstract public string __toString ( void )
}
Sign up to request clarification or add additional context in comments.

Comments

1

The Exception class is just container the programmer uses to pass information about the exception (an error code, a message, the location, the stack trace, other information that might be useful to handle the exception) from one part of the program (where the exception happened) to other part of the program (where there is code that knows how to handle it).

Its constructor does nothing more than storing the values it receives as arguments. The other methods are just getters; they are used to extract the values from the Exception object. The Exception class doesn't implement any behaviour.

Apart from the requirement to extend Exception, there is nothing special about it.

1 Comment

Does this suggest that Exceptions are recognised by PHP at parse time as special than being just a class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.