almost 9 years ago
In JavaScript try/catch/finally are called exception handling statements. In JavaScript try/catch/finally statement manage some or all of the errors that might occur in a block of code. Try/catch/finally in JavaScript lets you deal with exceptions gracefully. In JavaScript code whenever the browser runs into an exception, while aborting the execution of the remaining code, it displays an error message to the user. You can put the code in try/catch/finally and handle the exception. Basically you have to use try/catch to try and run some code, and find the exceptions in the code.
SYNTAX
- try {
- tryCode -The code which you want to try
- }
- catch(err) {
- catchCode - Block of code to handle errors
- }
- finally {
- finallyCode - Block of code to be executed regardless of the try / catch result
- }
try { tryCode -The code which you want to try } catch(err) { catchCode - Block of code to handle errors } finally { finallyCode - Block of code to be executed regardless of the try / catch result }
try statements
The statements to be executed.
catch(err)
Statements that are executed if an exception is thrown in the try block.
finally statements
When the try statement completes these Statements that are executed . These statements execute regardless of whether or not an exception was thrown or caught.
DESCRIPTION
Exception handling consist of the try statement which consists of a try block, it contains one or more statements, and at least one catch clause or a finally clause, or both. Basically there are three forms of the try statement:
After the execution of try and catch block the finally clause in JavaScript gets executed but before the statements following the try statement. The execution of try block is must, regardless of whether or not an exception is thrown or caught.
You can use multiple try statements. If an internal try statement does not have a catch clause, the encompass try statement's catch clause is entered.
JavaScript exceptions could be handled by the use of try statement.
Can you help out the community by solving one of the following Javascript problems?
Do activity (Answer, Blog) > Earn Rep Points > Improve Rank > Get more opportunities to work and get paid!
For more topics, questions and answers, please visit the Tech Q&A page.
0 Comment(s)