Web Development

Throw Try Catch

Throw Try Catch is section 20 of Beginner’s Guide to JavaScript.

Throw, Try and catch are statements in JavaScript which deals with Errors.

  • The try statement lets you test a block of code for errors.
  • The catch statement lets you handle the error.
  • The throw statement lets you create custom errors.

And also there is another statement finally comes after the above statements addition to that called ‘finally’. The finally statement lets you execute code, after try and catch, regardless of the result.

When executing JavaScript code, different errors can occur. Errors can be coding errors made by the programmer, errors due to wrong input, and other unforeseeable things. For example just try to execute the following program.

[code language=”html”]



[/code]

In the example above we have made a typo in the code in the try block. The catch block catches the error, and executes code to handle it. So the program will not be stucked in there. Just try to execute the same program without try and catch. You’ll see how it stuck in there.

[code language=”html”]



[/code]

JavaScript try and catch

The JavaScript statements try and catch come in pairs. The try statement allows you to define a block of code to be tested for errors while it is being executed. The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.

try {

              Block of code to try
}
catch(err) {
              Block of code to handle errors
}

JavaScript Throw

When an error occurs, JavaScript will normally stop, and generate an error message. We call this scenario as JavaScript Throws an error. With using a throw statement it allows you to create a custom error. We call this scenario as JavaScript throw an exception.

The exception can be a JavaScript String, a Number, a Boolean or an Object.

throw “Too big”;    // throw a text
throw 500;          // throw a number

If you use throw together with try and catch, you can control program flow and generate custom error messages.

The JavaScript finally Statement

The finally statement lets you execute code, after try and catch, regardless of the result.

try {
          Block of code to try
}
catch(err) {
          Block of code to handle errors
}
finally {
         Block of code to be executed regardless of the try / catch result
}

Just look at the following code segment which used to validate a input.

This example examines input. If the value is wrong, an exception (err) is thrown. The exception (err) is caught by the catch statement and a custom error message is displayed. So what ever happens, by the finally clause, it will clears the input box.

 

[code languague=”html”]

Please input a number between 5 and 10:




[/code]


What’s Next?

Learn what is JavaScript Debugging

Join Our Newsletter Today

Stay updated with all latest updates.
Give it a try, you can unsubscribe anytime.
Subscribe
SUBSCRIBE NOW

About the author

Sandeep Likhar

Sandeep Likhar from Indore India, Blogger, eBook Designer and Founder of LetsDnD. Having five years of experience in industry as a Digital Publishing Expert and eBook Converter providing services to Authors, Publishers and distributors worldwide. Proficient in converting books into various formats HTML, epub, mobi, word, PDF including all major online platforms iTunes, Kobo, Kindle, Createspace, B&N, Smashwords and more.

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz
DON’T MISS OUT!
Subscribe To Newsletter
Be the first to get latest updates and exclusive content straight to your email inbox.
Stay Updated
Give it a try, you can unsubscribe anytime.

Join Our Newsletter Today

Stay updated with all latest updates.
Give it a try, you can unsubscribe anytime.
Subscribe
SUBSCRIBE NOW