شرح صفوف ومحتوى الفقرة
عند انتقال الخطأ إلى catch (err) يمرر المحرك كائناً منظماً يحمل خاصيتين قياسيتين: err.name (نوع الخطأ مثل "TypeError") وerr.message (الوصف النصي للسبب):
| نوع الخطأ القياسي | متى يقع؟ | مثال مسبب |
|---|---|---|
ReferenceError | محاولة قراءة اسم متغير غير معرف أصلاً | console.log(missingVar) |
TypeError | تنفيذ عملية على نوع بيانات لا يقبلها | const x = null; x.trim() |
RangeError | تمرير رقم يتجاوز النطاق المسموح به | (12.5).toFixed(500) |
JavaScript
const rawCoupon = null;
let statusLine = '';
try {
statusLine = rawCoupon.toUpperCase();
} catch (err) {
statusLine = `Recovered from ${err.name} — using default code`;
}
document.getElementById('err-out').textContent = statusLine;