Subjects

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

What is the difference between catching Exception and catching Throwable? Exception और Throwable catch करने में क्या अंतर है?

Answer

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 typeCatches Exception subclasses?Catches Error subclasses?
catch (Exception $e)YesNo
catch (Throwable $e)YesYes

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 typeException 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?