জাভাস্ক্রিপ্ট এর finally স্টেটমেন্ট, জাভাস্ক্রিপ্ট কোড execute করে, try এবং catch স্টেটমেন্ট execute করার পরেও। নিচে জাভাস্ক্রিপ্ট এর try, catch এবং finally স্টেটমেন্ট ব্যবহার করে একটি ব্যাবহারিক উদাহরণ দেখুন।
Input a number between 5 and 10
জাভাস্ক্রিপ্ট এর finally স্টেটমেন্ট, জাভাস্ক্রিপ্ট কোড execute করে, try এবং catch স্টেটমেন্ট এর ফলাফল execute করার পরেও। নিচে জাভাস্ক্রিপ্ট এর finally স্টেটমেন্ট এর সিনট্যাক্স দেখুন।
try { Block of code to be try } catch(err) { Block of code to be handle errors } finally { Block of code to be executed regardless of the try / catch result }
try এবং catch স্টেটমেন্ট এর সাথে জাভাস্ক্রিপ্ট এর finally স্টেটমেন্ট ব্যবহার করে নিচে একটি উদাহরণ দেখুন।
<!DOCTYPE html> <html> <head> <title>JavaScript finally Statement</title> </head> <body> <p>Input a number between 5 and 10</p> <input id="demo" type="text"> <button type="button" onclick="myFunction()">Submit Input</button> <p id="resultV"></p> <script> function myFunction() { var message, x; message = document.getElementById("resultV"); message.innerHTML = ""; x = document.getElementById("demo").value; try { if(x == "") throw "is empty"; if(isNaN(x)) throw "is not a number"; x = Number(x); if(x > 10) throw "is more then 10"; if(x < 5) throw "is less then 5"; } catch(err) { message.innerHTML = "The Input " + err; } finally { document.getElementById("demo").value = ""; } } </script> </body> </html>