How to Throw an Exception in JavaScript

If you've ever want to "throw" a tantrum while writing a script with the JavaScript programming language, it's perfectly understandable. Unfortunately, tantrums don't work well in JavaScript. You can certainly throw an exception, however, as long as you know where and why it's appropriate to do so.

Things You'll Need

  • JavaScript editor
  • Web browser
Show More

Instructions

  1. Throw an Exception in JavaScript

    • 1

      Understand what throwing an exception in JavaScript really is: another way of controlling how a JavaScript responds to data. An exceptions tells the web browser, "Hey--if a certain thing happens on this web page, then follow my command." You can "throw" all kinds of "exceptions," including errors, text, number, Boolean values and even objects.

    • 2

      Decide what kind of exception you want to throw. In the examples below, the exception will be an error named "whoops."

    • 3

      A response to something is needed so that the example script will have something to call an error. So go on and create a variable that stores a response. Example: X = prompt("Enter a number less than 5:",""). This example asks a visitor to enter a number less than 5 in a prompt box. If the answer is greater than 5, then the example script will have something to throw an exception (or error) at.

    • 4

      Decide when you want to throw the exception. Example: try
      }
      if(x>5)
      throw "whoops"

    • 5

      Catch the error if it occurs and store it in a new variable. Example: catch(wronganswer)

    • 6

      Tell the script how to respond to a caught error. Example: if(wronganswer == "whoops")
      alert("BZZZZzzzzZZZZTT!")

    • 7

      Study the following example that demonstrates throwing an exception in JavaScript. It will display "BZZZZzzzzZZZZTT" in an alert box if the prompt box response is greater than 5.
      x = prompt("Enter a number less than 5:","")
      try
      {
      if(x>5)
      throw "whoops"
      }
      catch(wronganswer)
      {
      if(wronganswer=="whoops")alert("BZZZZzzzzZZZZTT!")
      }

    • 8

      Copy the code above and paste it onto your web pages. You can simply replace the variables in the sample code with your own values.

Tips & Warnings

  • A "throw" must be written in lowercase only. Uppercase throws will generate errors.

Related Searches:

Comments

You May Also Like

  • How to Enable Javascript in Mozilla

    JavaScript is a scripting language that enables websites to have custom, dynamic content displayed in the midst of otherwise static HTML content....

  • How to Change JavaScript

    JavaScript is the most common language for programming websites. It allows for a level of interactivity that is not possible with HTML...

  • How to Throw Exception in Java

    Error handling provides a way for Java developers to program responses to software errors. When a user enters a wrong value or...

  • Java Exceptions Tutorial

    An exception occurs when an abnormal situation (such as an error or problem) occurs that a method is unable to handle. The...

  • How Do I Disable Javascript?

    To disable or enable JavaScript is pretty simple. Depending on which browser you use, the steps may vary slightly. What follows are...

  • Java Script Troubleshooting

    Java Script troubleshooting can be tedious. Mistakes are generally not easily spotted and can take a fair amount of time to track...

  • How to Write My Own Exception Handler in Java

    The Java language provides the exception model so developers can handle situations in which normal program execution goes wrong. There are many...

  • How to Write Text in JavaScript

    JavaScript allows you to dynamically write text onto the page, using the document.write and document.writeln functions.

  • How to Avoid Null Pointer Exception in Java

    Null pointer exceptions are errors thrown by the Java compiler when the programmer attempts to use a variable that hasn't been defined....

  • How to Copy a File in Jsp

    Java doesn't offer a neat and pretty one-liner for copying files. However, Java's file input-output (I/O) classes make it fairly easy to...

Related Ads

Featured