Bugs and errors
are inevitable in programming. A friend of mine calls them unknown features :). Call them whatever you want, but I honestly believe that bugs are one of the things that make our work as programmers interesting. I mean no matter how frustrated you might be trying to debug some code overnight, I am pretty sure you will have a good laugh when you find out that the problem was a simple comma you overlooked, or something like that. Although, an error reported by a
client will bring about more of a frown than a smile. That said, errors can be annoying and a real pain in the behind. That is why in this article, I want to explain something called try / catch in JavaScript. A try / catch block is basically used to handle errors in JavaScript. You use this when you don't want an error in your script to break your code. While this might look like something you can easily do with an if statement, try/catch gives you a lot of benefits beyond what an if/else statement can do, some of which you will see below. A try statement lets you test a block of code for errors. A catch statement lets you handle that error. For example:
This is basically how a try/catch is constructed. You put your code in the try block, and immediately if there is an error, JavaScript gives the catch statement control and it just does whatever you say. In this case, it alerts you to the error. All JavaScript errors are actually objects that contain two properties: the name (for example, Error, syntaxError, and so on) and the actual error message. That is why when we alert e, we get something like ReferenceError: getData is not defined. Like every other object in JavaScript, you can decide to access the values differently, for example e.name(ReferenceError) and e.message(getData is not defined). But honestly this is not really different from what JavaScript will do. Although JavaScript will respect you enough to log the error in the console and not show the alert for the whole world to see :). What, then, is the benefit of try/catch statements? How to use try/catch statementsThe throw StatementOne of the benefits of try/catch is its ability to display your own custom-created error. This is called In situations where you don't want this ugly thing that JavaScript displays, you can throw your error (an exception) with the use of the throw statement. This error can be a string, boolean, or object. And if there is an error, the catch statement will display the error you throw.
This is nice, right? But we can take it a step further by actually throwing an error with the JavaScript constructor errors. Basically JavaScript categorizes errors into six groups:
So with all this, we could easily throw an error like
And you can easily use this anywhere with So far we have learnt about try/catch and how it prevents our script from dying, but that actually depends. Let's consider this example:
But when you try it out, even with the try statement, it still does not work. This is because there are two main types of errors in JavaScript (what I described above –syntaxError and so on – are not really types of errors. You can call them examples of errors): parse-time errors and runtime errors or exceptions. Parse-time errors are errors that occur inside the code, basically because the engine does not understand the code. For example, from above, JavaScript does not understand what you mean by {{}}, and because of that, your try / catch has no use here (it won't work). On the other hand, runtime errors are errors that occur in valid code, and these are the errors that try/catch will surely find.
Believe it or not, the above is valid code and the try /catch will handle the error appropriately. The Finally statementThe finally statement acts like neutral ground, the base point or the final ground for your try/ catch block. With finally, you are basically saying no matter what happens in the try/catch (error or no error), this code in the finally statement should run. For example:
Nesting try blocksYou can also nest try blocks, but like every other nesting in JavaScript (for example if, for, and so on), it tends to get clumsy and unreadable, so I advise against it. But that is just me. Nesting try blocks gives you the advantage of using just one catch statement for multiple try statements. Although you could also decide to write a catch statement for each try block, like this:
In this case, there won't be any error from the outer try block because nothing is wrong with it. The error comes from the inner try block, and it is already taking care of itself (it has it own catch statement). Consider this below:
This code above works a little bit differently: the error occurs in the inner try block with no catch statement but instead with a finally statement. Note that try/catch can be written in three different ways: The finally statement for this inner try will definitely work, because like we said earlier, it works no matter what happens in try/catch. But even though the outer try does not have an error, control is still given to its catch to log an error. And even better, it uses the error we created in the inner try statement because the error is coming from there. If we were to create an error for the outer try, it would still display the inner error created, except the inner one catches its own error. You can play around with the code below by commenting out the inner catch.
The Rethrow ErrorThe catch statement actually catches all errors that come its way, and sometimes we might not want that. For example,
Let's assume for a second that the number inputted will be less than 5 (the purpose of "use strict" is to indicate that the code should be executed in "strict mode"). With strict mode, you can not, for example, use undeclared variables (source). I want the try statement to throw an error of y is not... when the value of y is greater than 5 which is close to impossible. The error above should be for y is not less... and not y is undefined. In situations like this, you can check for the name of the error, and if it is not what you want, rethrow it:
This will simply rethrow the error for another try statement to catch or break the script here. This is useful when you want to only monitor a particular type of error and other errors that might occur as a result of negligence should break the code. ConclusionIn this article, I have tried to explain the following concepts relating to try/catch:
Thank you for reading. Follow me on twitter @fakoredeDami. Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started What happens when you throw an error JavaScript?The throw statement throws a user-defined exception. Execution of the current function will stop (the statements after throw won't be executed), and control will be passed to the first catch block in the call stack. If no catch block exists among caller functions, the program will terminate.
Does throwing an error return JavaScript?throw Error() is like a Javascript string, a number, a boolean, or an object. It returns specific errors as defined in the message value which is passed as an argument.
What are the best practice of error handling in JS?Don't Overuse the “Try-Catch”
However, one of the common mistakes developers make is to overuse exception handling. Sometimes we do it to keep the code look consistent across different files and methods. But, unfortunately, these would cause adverse effects both for application performance and error detection.
Can you throw anything in JavaScript?You can throw anything, including numbers, strings, and Error objects.
|