throw
Misol
Bu misol kiritilgan ma’lumotni tekshiradi.
Agar qiymat noto‘g‘ri bo‘lsa, istisno (err) tashlanadi:
<p>Please input a number between 5 and 10:</p>
<input id="demo" type="text">
<button type="button" onclick="myFunction()">Test Input</button>
<p id="message"></p>
<script>
function myFunction() {
const message = document.getElementById("message");
message.innerHTML = "";
let x = document.getElementById("demo").value;
try {
if(x == "") throw "is Empty";
if(isNaN(x)) throw "not a number";
if(x > 10) throw "too high";
if(x < 5) throw "too low";
}
catch(err) {
message.innerHTML = "Input " + err;
}
}
</script>
O‘zingiz sinab ko‘ring »
Tavsif
throw operatori o‘zingizning (maxsus) xatoingizni yaratish imkonini beradi.
throw operatori xatoni tashlaydi (throw), ya’ni hosil qiladi.
Buning texnik atamasi:
throw operatori istisno tashlaydi (throws an exception).
Istisno JavaScript String, Number, Boolean yoki Object bo‘lishi mumkin:
throw "Too big"; // throw a text
throw 500; // throw a number
throw false; // throw a boolean
throw person; // throw an object
Eslatma
throw operatorini try va catch bilan birga ishlatish dastur oqimini boshqarish va maxsus xato xabarlarini yaratish imkonini beradi.
Shuningdek qarang:
Sintaksis
throw expression;
Parametrlar
| Parametr | Tavsif |
| expression | Majburiy. Tashlanadigan istisno. String, number, boolean yoki obyekt bo‘lishi mumkin |
Brauzerlarda qo‘llab-quvvatlash
break — ECMAScript3 (JavaScript 1999) imkoniyati.
U barcha brauzerlarda qo‘llab-quvvatlanadi:
| Chrome | Edge | Firefox | Safari | Opera |
W3Schools Pathfinder
Yutuqlaringizni kuzating – bu bepul!
