When executing JavaScript code, errors will most definitely occur. These errors can occur due to the fault from the programmer’s side or the input is wrong or even if there is a problem with the logic of the program. But all errors can be solved and to do so we use five statements that will now be explained.
The try statement lets you test a block of code to check for errors.
The catch statement lets you handle the error if any are present.
The throw statement lets you make your own errors.
The finally statement lets you execute code, after try and catch.
The finally block runs regardless of the result of the try-catch block.
Simple error example:
try { dadalert("Welcome Fellow Geek!"); } catch(err) { console.log(err); } |
In the above code we make use of ‘dadalert’ which is not a reserved keyword, and is neither defined hence we get the error.
Output:
![]()
Another Example:
function geekFunc() { let a = 10; try { alert("Value of variable a is : " + a ); } catch ( e ) { alert("Error: " + e.description ); } } geekFunc(); |
In the above code, our catch block will not run as there’s no error in the above code and hence we get the output ‘Value of variable a is: 10’.
Output:

Try and Catch Block:
The try statement allows you to check whether a specific block of code contains an error or not.
The catch statement allows you to display the error if any are found in the try block.
try {
Try Block to check for errors.
}
catch(err) {
Catch Block to display errors.
}
Example:
try { dadalert("Welcome Fellow Geek!"); } catch(err) { console.log(err); } |
Output:
![]()
Javascript Throws Block
The throw Statement.
When any error occurs, JavaScript will stop and generate an error message. The throw statement lets you create your own custom error. Technically you can throw your custom exception (throw an error). The exception
can be a JavaScript Number, String, Boolean or Object. By using throw together with try and catch, you
can easily control the program flow and generate custom error messages.
Example:
try { throw new Error('Yeah... Sorry'); } catch(e) { console.log(e); } |
Output:
![]()
The finally Block
The finally Statement runs unconditionally after the execution of the try/catch block. Its syntax is
try {
Try Block to check for errors.
}
catch(err) {
Catch Block to display errors.
}
finally {
Finally Block executes regardless of the try / catch result.
}
Example:
try { alert( 'try' ); } catch (e) { alert( 'catch' ); } finally { alert( 'finally' ); } |
Output:

The Finally Block can also override the message of the catch block so be careful while using it.
Recommended Posts:
- How to catch all JavaScript errors and send them to server?
- Catch and Throw Exception In Ruby
- Difference between try-catch and if-else statements in PHP
- Scala | Try-Catch Exceptions
- Generating Errors using HTTP-errors module in Node.js
- How to get JavaScript stack trace when throw an exception ?
- JavaScript | Generator.prototype.throw() Method
- Reject Vs Throw Promises in JavaScript
- Throw Keyword in Scala
- jQuery deferred.catch() Method
- How to log errors and warnings into a file in php?
- PHP | Types of Errors
- How do I get PHP errors to display?
- Exceptions Vs Errors in PHP
- How to Create Custom Errors using New Function in Golang?
- Handling Errors in R Programming
- How to handle errors in node.js ?
- Top 10 Tools That Every Web Developer Must Try Once
- How to display popup message when logged out user try to vote ?
- JavaScript Course | Understanding Code Structure in JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


