लोड हो रहा है...

JavaScript त्रुटियाँ

त्रुटि वह तरीका है जिससे फ़ंक्शन आगे बढ़ने से इनकार करता है। यह पेज { drink: "tea" } इस्तेमाल करता है, जिसमें size नहीं है। throw new Error("missing size") label को तुरंत रोक देता है, return से पहले। catch वह Error लेता है। पेज की पंक्ति caught / missing size पढ़ी जाती है।

यह डिबगिंग पेज नहीं है

पिछले पेज ने console.error("missing size") बुलाया और फिर एक запас पाठ लौटाया। स्क्रिप्ट चलती रही। कुछ फेंका नहीं गया। console.error फेंकता नहीं।
throw अलग है। उसी फ़ंक्शन में उसके बाद की पंक्तियाँ नहीं चलतीं। केवल catch संभाल सकता है। पिछली डिबगिंग पंक्ति fixed / tea / regular थी।

Error में दो स्ट्रिंग होती हैं

new Error("missing size") एक ऑब्जेक्ट बनाता है। error.name Error होता है। error.message दिया गया पाठ है, missing size। ऑब्जेक्ट छापने से इन दो फ़ील्ड को छापना ज़्यादा उपयोगी है।

catch Error लेता है

size न हो तो label फेंकता है। कॉल try के अंदर है, इसलिए catch चलता है और error.message छापता है। पाठ missing size है।

गायब आकार को पकड़ो

javascript
function label(order) {
if (!order.size) {
throw new Error("missing size");
}
return order.drink + " / " + order.size;
}
try {
console.log(label({ drink: "tea" }));
} catch (error) {
console.log(error.message);
}

लॉग missing size है। label अपने return तक कभी नहीं पहुँचा, इसलिए यहाँ tea / missing size नहीं बनता।

अच्छा ऑर्डर catch छोड़ देता है

size को regular दो। label सामान्य लौटता है। catch नहीं चलता।

पूरा ऑर्डर

javascript
function label(order) {
if (!order.size) {
throw new Error("missing size");
}
return order.drink + " / " + order.size;
}
try {
console.log(label({ drink: "tea", size: "regular" }));
} catch (error) {
console.log(error.message);
}

लॉग tea / regular है।

finally दोनों रास्तों पर चलता है

finally सफलता पर try के बाद चलता है, और फेंकने पर catch के बाद। उसे उस कदम के लिए रखो जो दोनों मामलों में होना चाहिए।

throw के बाद finally

javascript
function label(order) {
if (!order.size) {
throw new Error("missing size");
}
return order.drink + " / " + order.size;
}
try {
label({ drink: "tea" });
} catch (error) {
console.log(error.message);
} finally {
console.log("done");
}

लॉग पहले missing size, फिर done हैं। फेंकने के बाद finally done लिखता है।

नाम और संदेश पढ़ो

वही Error पकड़ो और दोनों फ़ील्ड अलग पंक्तियों पर छापो।

नाम और संदेश

javascript
try {
throw new Error("missing size");
} catch (error) {
console.log(error.name);
console.log(error.message);
}

लॉग पहले Error, फिर missing size हैं। इस तरह error.name Error है और error.message missing size है।

जाँच फेंकती है, बुलाने वाला पकड़ता है

requireSize नियम जानती है। वह फेंकती है और लेबल नहीं बनाती। क्या छापना है, यह बुलाने वाला तय करता है।

जाँच अलग रखो

javascript
function requireSize(order) {
if (!order.size) {
throw new Error("missing size");
}
}
try {
requireSize({ drink: "tea" });
console.log("ok");
} catch (error) {
console.log(error.message);
}

लॉग missing size है। पंक्ति console.log("ok") नहीं चलती, क्योंकि requireSize का फेंकना catch पर कूद गया।

कप के नीचे की पंक्ति

पेज label को बिना आकार के बुलाता है। catch दिखने वाली पंक्ति संदेश से बनाता है।

पेज डेमो — पकड़ी गई त्रुटि

html
<!DOCTYPE html>
<html>
<body>
<p id="line">…</p>
<script>
function label(order) {
if (!order.size) {
throw new Error("missing size");
}
return order.drink + " / " + order.size;
}
let line = "";
try {
line = label({ drink: "tea" });
} catch (error) {
line = "caught / " + error.message;
}
document.getElementById("line").textContent = line;
</script>
</body>
</html>

पंक्ति caught / missing size पढ़ी जाती है। पूरा ऑर्डर tea / regular लौटाता और catch को बिना इस्तेमाल छोड़ता। fixed / tea / regular डिबगिंग पेज की पंक्ति थी।

काउंटर पर

  1. try के अंदर label({ drink: "tea" }) बुलाओ। catch का लॉग missing size है।
  2. label({ drink: "tea", size: "regular" }) बुलाओ। लॉग tea / regular है, और catch नहीं चलता।
  3. असफल कॉल के चारों ओर finally जोड़ो। दूसरा लॉग done है।
  4. error.name और error.message पढ़ो। लॉग Error और missing size हैं।
  5. पेज की पंक्ति पढ़ो: caught / missing size।

काउंटर पर चूक

throw फ़ंक्शन रोकता है। console.error न फेंकता है और न फ़ंक्शन रोकता है।

  • throw के बाद की पंक्तियों के चलने की उम्मीद करना
  • catch भूलकर त्रुटि को बिना पकड़े छोड़ना
  • यह सोचना कि finally केवल सफलता के बाद चलता है
  • error.message को नाम Error पढ़ना
  • पंक्ति को fixed / tea / regular पढ़ना

यह याद रखो

  • throw new Error("missing size") label को return से पहले रोकता है
  • catch Error लेता है और error.message छापता है, जो missing size है
  • पूरा ऑर्डर tea / regular लिखता है और catch छोड़ देता है
  • फेंकने के बाद finally done लिखता है
  • पेज की पंक्ति caught / missing size पढ़ी जाती है

🧠 अपने ज्ञान की परीक्षा करें

शुरू करने के लिए तैयार

अपने ज्ञान की परीक्षा करें

इस इंटरैक्टिव क्विज़ के साथ अपनी चुनौती लें और देखें कि आप विषय को कितनी अच्छी तरह समझते हैं

❓
5
प्रश्न
🎯
70%
पास करने के लिए
♾️
∞
समय
🔄
∞
प्रयास

📝 निर्देश

  • हर प्रश्न को ध्यान से पढ़ें
  • हर प्रश्न के लिए सबसे अच्छा उत्तर चुनें
  • आप जितनी बार चाहें क्विज़ दोबारा दे सकते हैं
  • आपकी प्रगति शीर्ष पर दिखाई जाएगी