Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 1 of 10 · Error & Exception Handling
Interview question

What is the difference between an error and an exception in PHP? PHP में error और exception में क्या अंतर है?

Answer

Since PHP 7, both Errors and Exceptions implement the Throwable interface, but they represent different kinds of problems.

AspectErrorException
RepresentsInternal engine problems - type mismatches, undefined functions, memory issuesApplication-level problems you anticipate and handle - invalid input, failed DB calls
Typically thrown byThe PHP engine itselfYour own code or a library
Common subclassesTypeError, DivisionByZeroError, ArgumentCountErrorInvalidArgumentException, RuntimeException, PDOException
Should you catch it?Sometimes, for graceful degradation, but often indicates a bug to fixYes, 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 करते हैं, पर ये अलग-अलग तरह की समस्याएं दर्शाते हैं।

पहलूErrorException
दर्शाता हैInternal engine समस्याएं - type mismatches, undefined functions, memory issuesApplication-level समस्याएं जो आप predict और handle करते हैं - invalid input, failed DB calls
आमतौर पर throwPHP engine खुदआपका कोड या कोई library
आम subclassesTypeError, DivisionByZeroError, ArgumentCountErrorInvalidArgumentException, 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?