What is the difference between catching Exception and catching Throwable? Exception और Throwable catch करने में क्या अंतर है?
Throwable is the top-level interface implemented by both Error and Exception. Catching Exception only catches application-level exceptions; catching Throwable catches everything, including engine-level Errors like TypeError.
try {
strlen(); // ArgumentCountError - an Error, not an Exception
} catch (Exception $e) {
echo 'This will NOT catch it';
} catch (Throwable $e) {
echo 'This WILL catch it: ' . $e->getMessage();
}| Catch type | Catches Exception subclasses? | Catches Error subclasses? |
|---|---|---|
| catch (Exception $e) | Yes | No |
| catch (Throwable $e) | Yes | Yes |
Interview tip: Recommend using catch (Throwable $e) only at the outermost boundary of an app (e.g. a global handler) to prevent white-screen crashes, not as a routine replacement for specific exception catching.
Throwable top-level interface है जिसे Error और Exception दोनों implement करते हैं। सिर्फ Exception catch करने से application-level exceptions ही मिलते हैं; Throwable catch करने से TypeError जैसे engine-level Errors भी मिलते हैं।
try {
strlen(); // ArgumentCountError - एक Error, Exception नहीं
} catch (Exception $e) {
echo 'यह इसे catch नहीं करेगा';
} catch (Throwable $e) {
echo 'यह catch करेगा: ' . $e->getMessage();
}| Catch type | Exception subclasses catch? | Error subclasses catch? |
|---|---|---|
| catch (Exception $e) | हाँ | नहीं |
| catch (Throwable $e) | हाँ | हाँ |
इंटरव्यू टिप: सलाह दें catch (Throwable $e) सिर्फ app की सबसे बाहरी boundary पर इस्तेमाल करें (जैसे global handler) white-screen crashes रोकने के लिए, specific exception catching की रोज़मर्रा जगह पर नहीं।
Was this answer clear?