Thursday , 17 August 2017
Home / Programming / JavaScript / Try-catch Statement – Advanced JavaScript Tutorial
Try-Catch Statement
Try-Catch Statement

Try-catch Statement – Advanced JavaScript Tutorial

The try-catch statement is a unique way to handle errors. When errors occur, usually it would be logged into the console for you to see. This can be accessed in a variety of ways in different browsers. Usually you’ll find it in the developer section. The try-catch statement allows you to handle errors in your own way.

Let’s clean up our document before we move on. Update your document to this.

Add this bit of code into your script.

To use the try-catch statement, you first type in try. The block of code inside here will always execute. In our example, we output a variable called totallyRandom into the document. However, this variable doesn’t exist because we never created it. This will lead to an error.

The catch statement has 1 parameter. This is the error object which contains info about our current error. This block of code will only execute if the code inside the try statement gives out an error. In that case, we output a message into the document. The error object will contain various properties depending on the browser, but all browsers give a name and message property.

Alright, at this point you may be asking yourself what’s the point of this? By using try-catch statements, you can personally log the error into a database or try executing a different bit of code. Browsers don’t do this by default. The typical user doesn’t know what to do with an error when they see one. They might end up getting frustrated and leave.

So, when should I use try-catch statements and how often can I use them?

They can be used anytime you want. There’s a lot of debate whether you should use them in live production. Not only that, try-catch statements can show a performance issue if you use a lot of them. You should ONLY use them when you really need to catch an error and plan on doing something about it. This is only when you know an error is possible in your code.

Typically things like a variable not existing or a function not running isn’t the kind of thing you would use a try-catch statement for. It’s really only useful for debugging code.

Finally Statement

The finally statement is an optional block of code you can run whether there is an error or not. Update your script to this.

The code in the finally statement is guaranteed to run no matter what. If there’s an error, it will run. If there’s no error, then it will still run.

Conclusion

Hopefully you understand the try-catch statement so that you can debug your code. For more information on the try-catch statement, then go here.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try…catch

About Jasko Koyn

Check Also

Audio

Audio – Advanced JavaScript Tutorial

The video and audio objects both have the same events, properties and methods. So, you …

Leave a Reply

Your email address will not be published. Required fields are marked *

ULTIMATE WORDPRESS DEVELOPMENT COURSE
Want to learn how to create your own custom WordPress plugins and themes? Check out my course!
Get 93% off my course. You'll also learn how to integrate WooCommerce and BuddyPress into your projects. This is a limited time offer
Yes, I would like to get 93% off your course!
No Thanks!
close-link