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 है।
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 नहीं चलता।
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 के बाद। उसे उस कदम के लिए रखो जो दोनों मामलों में होना चाहिए।
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 पकड़ो और दोनों फ़ील्ड अलग पंक्तियों पर छापो।
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 नियम जानती है। वह फेंकती है और लेबल नहीं बनाती। क्या छापना है, यह बुलाने वाला तय करता है।
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 दिखने वाली पंक्ति संदेश से बनाता है।
<!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 डिबगिंग पेज की पंक्ति थी।
काउंटर पर
tryके अंदरlabel({ drink: "tea" })बुलाओ। catch का लॉग missing size है।label({ drink: "tea", size: "regular" })बुलाओ। लॉग tea / regular है, और catch नहीं चलता।- असफल कॉल के चारों ओर
finallyजोड़ो। दूसरा लॉग done है। error.nameऔरerror.messageपढ़ो। लॉग Error और missing size हैं।- पेज की पंक्ति पढ़ो: caught / missing size।
काउंटर पर चूक
throw फ़ंक्शन रोकता है। console.error न फेंकता है और न फ़ंक्शन रोकता है।
throwके बाद की पंक्तियों के चलने की उम्मीद करनाcatchभूलकर त्रुटि को बिना पकड़े छोड़ना- यह सोचना कि
finallyकेवल सफलता के बाद चलता है error.messageको नामErrorपढ़ना- पंक्ति को fixed / tea / regular पढ़ना
यह याद रखो
throw new Error("missing size")labelकोreturnसे पहले रोकता हैcatchError लेता है औरerror.messageछापता है, जो missing size है- पूरा ऑर्डर tea / regular लिखता है और
catchछोड़ देता है - फेंकने के बाद
finallydoneलिखता है - पेज की पंक्ति caught / missing size पढ़ी जाती है
🧠 अपने ज्ञान की परीक्षा करें
अपने ज्ञान की परीक्षा करें
इस इंटरैक्टिव क्विज़ के साथ अपनी चुनौती लें और देखें कि आप विषय को कितनी अच्छी तरह समझते हैं
📝 निर्देश
- हर प्रश्न को ध्यान से पढ़ें
- हर प्रश्न के लिए सबसे अच्छा उत्तर चुनें
- आप जितनी बार चाहें क्विज़ दोबारा दे सकते हैं
- आपकी प्रगति शीर्ष पर दिखाई जाएगी