0% completed
try...catch
On This Page
The try...catch statement
Errors thrown inside a called function
Catch without a parameter
What try...catch cannot catch
When to use try...catch
The program below reads an order that a customer typed by hand. The customer forgot the quotation marks around one property name, so the text is not valid JSON.
Reading the order SyntaxError: Expected double-quoted property name in JSON at position 17 (line 1 column 18)
JSON.parse throws a SyntaxError, and nothing after it runs: neither the item nor Done is printed. When nothing handles an error, the engine stops the script that threw it. In Node.js the whole process ends; in a browser, the current script stops while the page stays open. One bad order should not stop a shop, so the program needs a way to notice the error and continue.
The try...catch statement
A try...catch statement runs a block of code and gives you a second block to run if the first one throws an error.
try { // code that might throw an error } catch (error) { // code that runs only if the try block threw an error }
The engine runs the try block line by line. If every line succeeds, it skips the catch block. If a line throws an error, the engine stops the try block at that line, skips the rest of it, and runs the catch block. The parameter, usually named error, holds the error object that was thrown. After either block finishes, the program continues with the next statement.
The diagram follows both paths through the next program, which puts the risky lines of the same order inside a try block. The left column shows the path with no error, where the catch block is skipped. The right column shows the path this program takes: JSON.parse throws, the rest of the try block is skipped, and catch runs with the error object.
Reading the order Could not read the order. Name: SyntaxError Message: Expected double-quoted property name in JSON at position 17 (line 1 column 18) Done
The call to JSON.parse throws, so the line that prints the item is skipped and the catch block runs instead. This time Done is printed, because the error was handled and the script continued. The message text comes from the engine, so another browser or Node.js version may word it differently.
Errors thrown inside a called function
The try block does not need to contain the line that fails. An error thrown inside a function is passed to the code that called the function, then to the code that called that code, until some caller has a try block around the call. In the next program, JSON.parse fails inside readQuantity, but the try block that handles the error is in showQuantity.
Quantity: 2 Bad order text: SyntaxError Done
Follow the second call in the diagram. JSON.parse throws, so readQuantity stops without returning a value, and the line in showQuantity that would print the quantity never runs. The engine looks for the nearest enclosing try block, finds the one in showQuantity, and runs its catch block. If no function on the way had a try block, the script would stop as it did in the first program.
Catch without a parameter
When the catch block does not need the error object, you can leave out the parameter and its parentheses. This short form is supported by all current browsers and Node.js versions.
true false
What try...catch cannot catch
A try block handles only runtime errors that happen while the block is running. Two kinds of errors are outside that limit:
- A syntax error in the script itself. The engine parses the whole script before it runs any of it, so a
tryblock inside a script with a syntax error never runs. - An error in a function that runs later. If the
tryblock schedules a function withsetTimeout, thetryblock has finished by the time that function runs. An error inside the function is not caught by thattryblock.
When to use try...catch
Wrap code in try...catch when it can fail for reasons outside your control, such as text typed by a user, data from a file, or a reply from another system. A try block also has a cost. It catches every runtime error inside it, including errors caused by bugs in your own code.
For example, if the line in showQuantity that prints the quantity misspelled quantity as quantty, the program would print Bad order text: ReferenceError. The order text was fine, but the message blames it. To avoid this, keep try blocks small, put only the risky lines inside them, and print error.name and error.message while you develop, so that you can see when an unexpected type of error was caught.
Reading Progress
0%
On This Page
The try...catch statement
Errors thrown inside a called function
Catch without a parameter
What try...catch cannot catch
When to use try...catch