What is the difference between an error and an exception in PHP? PHP में error और exception में क्या अंतर है?
Since PHP 7, both Errors and Exceptions implement the Throwable interface, but they represent different kinds of problems.
| Aspect | Error | Exception |
|---|---|---|
| Represents | Internal engine problems - type mismatches, undefined functions, memory issues | Application-level problems you anticipate and handle - invalid input, failed DB calls |
| Typically thrown by | The PHP engine itself | Your own code or a library |
| Common subclasses | TypeError, DivisionByZeroError, ArgumentCountError | InvalidArgumentException, RuntimeException, PDOException |
| Should you catch it? | Sometimes, for graceful degradation, but often indicates a bug to fix | Yes, this is the expected way to handle recoverable failures |
Interview tip: Mention that both extend the Throwable interface, so a single catch (Throwable $e) block can catch either - useful as a last-resort safety net at the top of an application.
PHP 7 से Error और Exception दोनों Throwable interface implement करते हैं, पर ये अलग-अलग तरह की समस्याएं दर्शाते हैं।
| पहलू | Error | Exception |
|---|---|---|
| दर्शाता है | Internal engine समस्याएं - type mismatches, undefined functions, memory issues | Application-level समस्याएं जो आप predict और handle करते हैं - invalid input, failed DB calls |
| आमतौर पर throw | PHP engine खुद | आपका कोड या कोई library |
| आम subclasses | TypeError, DivisionByZeroError, ArgumentCountError | InvalidArgumentException, RuntimeException, PDOException |
| Catch करें? | कभी-कभी, graceful degradation के लिए, पर अक्सर fix करने लायक bug दर्शाता है | हाँ, recoverable failures handle करने का सही तरीका |
इंटरव्यू टिप: बताएं दोनों Throwable interface extend करते हैं, तो एक catch (Throwable $e) block दोनों catch कर सकता है - application के top level पर एक safety net के रूप में उपयोगी।
Was this answer clear?