try, catch, and finally
In JavaScript programming try, catch and finally block methods are applied for exception handling in the program which helps to control by catching and managing the program errors that occur during the execution of a particular block section of a program source code in JavaScript. Here try, catch and finally block methods help the programmer to ensure that the program or application designed by the programmer does not stop or block due to any kind of error, whereas try, catch and finally block methods provide advanced control to the programmer over the default Behaviors of your program flow.

So, let’s know more about try, catch and finally block methods in JavaScript programming.
try Block Method in JavaScript.
In JavaScript program try block method contains the program source code that can generate any kind of error or exception in the current program. Here if any error is generated in the created try source block, then the control flow of the current program is transferred to the immediate catch block. If no error is generated in the created program source code, then the catch block portion is ignored.
Syntax of try Block Method.
try {
// create a program source Code that may be throw an error
}
catch Block Method in JavaScript.
In any JavaScript program, the catch block method is applied in the try block to control or manage the generated program source code exception or error. If any error is generated in the existing program, then it is moved to the catch block, where the programmer can control or manage it, can log that error or exception in the system.
Syntax of catch block method.
catch (error) {
// write a program source Code that handles the error in catch block
}
The created catch block method has a user defined optional parameter set, known as error in the catch block, which holds the program source error element thrown in the try block method. It provides the current program object error information, such as the error message, stack trace, etc.
finally Block Method in JavaScript.
In any developed JavaScript program, finally block is implemented after the try and catch block method execution, whether an error is thrown in the current program or not. Where finally block is important for clean-up task activities in the program, such as closing files in the program, allocating system resources, or logging the final system message, etc., which are required by the programmer irrespective of whether an exception or error occurs in the program.
Syntax of finally Block Method.
finally {
// create a program source Code that will always run after try-catch block method
}
Example of try, catch and finally method in JavaScript.
try {
const output = 24 / 0; // here the output variable throw an error division with 0 here not possible
console.log(“The output is -“, output); // the code of program not execute due to error
} catch (error) {
console.log(“Let used t catch error”, error.message); // we use catch block to Catching the error for log
} finally {
console.log(“We use the finally block, and it runs after tray-catch block to catch of error”);
}
finally Block Method Explanation.
Here try block method contains the program source code that can generate an error in the current program, in the current condition, by dividing the output variable by zero.
Here the catch block method catches the error developed in the current program source code and logs it in the program.
Finally, the finally block method will always execute whether the error is developed in the current program or not.
When to apply finally block method in JavaScript program?
The finally block method is applied in JavaScript programs when the programmer needs to decide that the program should continue running no matter what happens in the source code of the current program. For example,
- Closing files or database connections in a program.
- Stopping animations in a program or clearing event listeners.
- Logging messages in the system.
Example with try-catch and finally block method cleanup.
let connect = null;
try {
connect = openDatabaseConnect(); // we use function here to deal with a database connection
// here Some database may be produce or throw an error
const db = connect.query(“SELECT * FROM employee”);
} catch (error) {
console.log(“unable to access database query”, error.message);
} finally {
if (connect) {
connect.close(); // it used to define db connection is closed
console.log(“it terminate Database connection”);
}
}
Here in this program example.
If any kind of error is generated while communicating or querying with the current employee database then it is caught in the catch block method in the program.
Here finally block method ensures that the current database connection is always closed whether any error occurs in the program or not.
Finally block method and return statement in JavaScript.
In a JavaScript program, the finally block source code will run even if a return statement is found in a try block method or a catch block. This is important for the programmer to know, as it may lead to abnormal program Behavior. Especially, when the programmer is using the return statement for immediate exit from the function code in a program.
Example of a return statement in a try block or catch method.
function multitestblock() {
try {
console.log(“now you in try block method”);
return “it return value from try block”;
} catch (error) {
console.log(“now you are In catch block method”);
return “it return value from catch block”;
} finally {
console.log(“now you are in finally block method”);
}
}
console.log(multitestblock()); // it used to Logs the message from try block, finally block, and return statement value
Here in this program, the return statement in the try block method is executed first, however, the finally block runs before the function actually returns a value.
Summary of try, catch, and finally block methods.
- try block method – The try block method contains program source code that can generate errors in the current program.
- catch block method – The catch block catches program errors generated in the try block in the current program, and manages and controls them
- finally block method – The finally block executes program source code in the current program that must run regardless of whether errors are generated in the program or not. For example, it is very useful for clean-up tasks or activities like closing database files or releasing resources.
- Error Re-Throwing Concept – Programmers use the throw keyword to re-throw program errors caught in the catch block so that these errors can be controlled or managed at a higher level in the current program.
- Specific error handling – By applying the instanceof method in a JavaScript program, programmers can manage multiple type errors in multiple steps order.