JavaScript throw 语句

পরিভাষা ও ব্যবহার

throw 语句 এররর তোলে (উৎপন্ন করে)।

যখন এররর ঘটে, JavaScript সাধারণত থামে এবং এররর সংবাদ তৈরি করে।

এর প্রযুক্তিগত শব্দটি হল: JavaScript এররর তোলতে (throw) দেয়।

throw 语句 এটি আপনাকে কাস্টম এররর তৈরি করতে দেয়।

Its technical term is: throw an exception (exception).

An exception can be a JavaScript string, number, boolean, or object:

throw "Too big";    // Throw text
throw 500;          // Throw a number

When throw is used with try and catch, you can control the program flow and generate custom error messages.

For more information on JavaScript errors, please learn our JavaScript error tutorial.

Example

This example checks the input. If the value is wrong, it throws an exception (err).

catch statement catches an exception (err) and displays a custom error message:

<!DOCTYPE html>
<html>
<body>
<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() {
  var message, x;
  message = document.getElementById("message");
  message.innerHTML = "";
  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>
</body>
</html>

try it yourself

grammar

throw expression;

parameter value

parameter description
expression প্রয়োজনীয়, ফেলতে হবে অসুবিধা। এটি হতে পারে স্ট্রিং, সংখ্যা, বুল বা অবজেক্ট。

প্রযুক্তিগত বিবরণ

JavaScript সংস্করণ: ECMAScript 3

ব্রাউজার সমর্থন

বিন্যাস Chrome IE Firefox Safari Opera
throw সমর্থন সমর্থন সমর্থন সমর্থন সমর্থন

সংশ্লিষ্ট পৃষ্ঠা

JavaScript শিক্ষা:JavaScript ত্রুটি

JavaScript পরামর্শপত্র:JavaScript try/catch/finally বিন্যাস