Did this article
help you?

This helped me

How to use Try Catch in Javascript: Easy Walkthrough

This How.com.vn will teach you how to use the try/catch code in Javascript. "Try" stands for the code you want to attempt to complete while "catch" is the code that runs through if there is an error found. For example, the "catch" could be showing "Username cannot contain special characters (!?*)" when a user tries to enter special characters into the username field.

Steps

  1. How.com.vn English: Step 1  Open a JavaScript editor.
    Notepad and TextEdit are for the default text-editing softwares on Windows and Mac, but you can use software like Visual Studio, if you have it.
    • To open Notepad on Windows, press the Windows key, type in "Notepad," and click the text editor search result.
    • To open TextEdit on Mac, click the magnifying glass icon, type "TextEdit" and click the text editor search result.
  2. How.com.vn English: Step 2  Create an HTML document.
    Add the following code to your document:
    <!DOCTYPE html><html><body></body></html>
  3. How.com.vn English: Step 3  Enter the following example code between the open (<body>) and closed (</body>) tags:
    the following example code is from https://www.w3schools.com/jsref/jsref_try_catch.asp.
    <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>
    • In this example, the website is asking the user to enter a number 5-10. The "try" code is set to output specific variables depending on the input (Is there an entered variable? Is the entered variable a number? Is it greater than 10? Is it less than 5?) whereas the "catch" code is set to return the user with an error response if none of the conditions in the "try" code are met.
    • Save this document as an HTML file and you'll be able to see it in your browser. Go to File > Save As and change the file type to HTML in the drop-down box next to "Save as type." Right-click the file then select Open with > Chrome (or whatever web browser you want to use) and that code should run in your browser.
    Advertisement


Expert Q&A

Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit

      Advertisement

      About this article

      How.com.vn English: Darlene Antonelli, MA
      Written by:
      How.com.vn Technology Writer
      This article was co-authored by How.com.vn staff writer, Darlene Antonelli, MA. Darlene Antonelli is a Technology Writer and Editor for How.com.vn. Darlene has experience teaching college courses, writing technology-related articles, and working hands-on in the technology field. She earned an MA in Writing from Rowan University in 2012 and wrote her thesis on online communities and the personalities curated in such communities.
      How helpful is this?
      Co-authors: 3
      Updated: January 6, 2021
      Views: 867
      Article SummaryX

      1. Open a JavaScript editor.
      2. Create an HTML document.
      3. Enter the following example code between the open and closed tags.

      Did this summary help you?

      Thanks to all authors for creating a page that has been read 867 times.

      Is this article up to date?

      ⚠️ Disclaimer:

      Content from Wiki How English language website. Text is available under the Creative Commons Attribution-Share Alike License; additional terms may apply.
      Wiki How does not encourage the violation of any laws, and cannot be responsible for any violations of such laws, should you link to this domain, or use, reproduce, or republish the information contained herein.

      Notices:
      • - A few of these subjects are frequently censored by educational, governmental, corporate, parental and other filtering schemes.
      • - Some articles may contain names, images, artworks or descriptions of events that some cultures restrict access to
      • - Please note: Wiki How does not give you opinion about the law, or advice about medical. If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.
      • - Readers should not judge the importance of topics based on their coverage on Wiki How, nor think a topic is important just because it is the subject of a Wiki article.

      Advertisement