JavaScript try catch Statement
JavaScript try catch Statement
The try
statement defines a block of code that you want to try to execute, and use the catch
statement to define a block of code that will execute if an exception to the normal running of the code occurs in the block of code defined by the try
statement.
The following are the key points to be noted while using try catch
statement.
- The
try...catch
statements work as a pair; you can’t have one without the other. - If no exception occurs inside the
try
block, the code inside the catch statement is never executed. - The
catch
statement also enables you to get the contents of the exception message that would have been shown to the user had you not caught it first.
Example
Syntax
Without Using try catch Block
Unlike the above example, the following example throws an error and stop executing further.
Example
Reminder
Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.
We are working to cover every Single Concept in JavaScript.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.