JS साइलेंट Errors
In this page:
Error को "Silent" क्या बनाता है
एक silent error कोई crash नहीं देता, कोई दिखने वाला error message नहीं, और कोई साफ संकेत नहीं कि कुछ गलत हुआ -- code बस वह नहीं करता जो इरादा था, जो समस्या की ओर इशारा करते एक साफ stack trace वाले ज़ोरदार crash से debug करना कहीं ज़्यादा उलझाने वाला हो सकता है।
उदाहरण: What Makes an Error "Silent"
function divide(a, b) {
if (b === 0) return; // no crash, no error - just wrong silently
return a / b;
}
console.log(divide(10, 0)); // undefined, no indication of why
Unhandled Promise Rejections
बिना .catch() के reject होने वाला एक Promise (या एक async function जिसकी error कभी catch नहीं होती) एक "unhandled rejection" पैदा करता है -- browser आमतौर पर console में एक warning log करता है, पर चल रही application में कुछ भी असल में react नहीं करता या failure को user तक नहीं पहुंचाता।
उदाहरण: Unhandled Promise Rejections
Promise.reject("Something failed"); // no .catch() - unhandled rejection warning only, nothing reacts
खाली या बहुत ज़्यादा Broad Catch Blocks
एक catch block जो caught error के साथ कुछ नहीं करता -- catch (e) {} -- failure को पूरी तरह दबा देता है, असल में उसे नज़र से छुपा देता है।
यह कभी-कभी किसी genuinely optional operation के लिए जानबूझकर किया जाता है, पर कहीं ज़्यादा बार यह गलती से किसी investigate करने लायक असली समस्या को छुपा देता है।
उदाहरण: Empty or Overly Broad Catch Blocks
try {
JSON.parse("bad json");
} catch (e) {} // swallowed silently, hides a real problem
console.log("Continues as if nothing happened");
Optional Chaining और ?? से Silent Failures
Optional chaining (?.) और nullish coalescing (??) genuinely optional data के लिए बेहतरीन हैं, पर इन्हें हर जगह ज़्यादा इस्तेमाल करना उस स्थिति को छुपा सकता है जहाँ किसी value का गायब होना असल में एक असली bug दिखाता है -- अनपेक्षित condition को सामने लाने के बजाय चुपचाप undefined या किसी default पर fallback होते हुए।
उदाहरण: Silent Failures from Optional Chaining and ??
const user = { profile: null };
console.log(user.profile?.name ?? "Unknown"); // masks whether this is expected or a real bug
Silent Failures का पता लगाना
silent failures को सामने लाने के लिए खासतौर पर tools मौजूद हैं: browser का unhandledrejection event बिना .catch() वाली किसी भी Promise rejection के लिए fire होता है, और linters खाली catch blocks या गायब return statements को अपने आप flag कर सकते हैं, कई silent-failure patterns को production तक पहुंचने से पहले ही पकड़ते हुए।
उदाहरण: Detecting Silent Failures
window.addEventListener("unhandledrejection", (e) => console.log("Caught silent rejection:", e.reason));
Promise.reject("oops");
- बिना .catch() (या await के चारों ओर try/catch) के कोई async function या Promise chain लिखना, जिससे एक rejection एक visible error की तरह दिखने के बजाय चुपचाप गायब हो जाती है।
- optional chaining (?.) का इतना ज़्यादा इस्तेमाल करना कि कोई genuinely unexpected missing property अपने असली source के पास एक helpful error की तरह दिखने के बजाय चुपचाप undefined return कर दे।
- किसी error को broadly catch करना और उसके साथ कुछ न करना (एक empty catch block), असल में उस information को swallow करते हुए जो किसी असली problem को diagnose करने में मदद करती।
- एक silent error वह है जो बिना किसी visible crash, warning, या console output के fail हो जाती है -- अक्सर एक loud error से notice और debug करना ज़्यादा मुश्किल होती है।
- एक unhandled Promise rejection (कोई .catch() नहीं) asynchronous code में silent failures का एक बहुत common source है।
- एक empty या बहुत ज़्यादा broad catch block गलती से genuinely useful error information को suppress कर सकता है।
browser का unhandledrejection event और silent failures के लिए console warnings हर modern browser में supported हैं, उन issues को surface करने में मदद करते हुए जो वरना notice नहीं होते।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: