The program can be stopped due to a mistake in one of the programs, also due to the wrong input of the user and the program can be stopped, even if the server does not respond properly, the program can be stopped, the execution of the program can be stopped due to many reasons. Basically error handling to save.
Suppose a programming of 100 lines catches an error in line 3, then what will happen? The program will stop on that line, the other line will no longer work, number 4–100: the line will no longer work.
In that case, if you use error handling, the program will not stop at line 4–100 they will work, that is why it basically uses error handling.
Some keywords are used for error handling, there’s a syntax construct try … catch that allows us to “catch” errors so the script can, instead of dying, do something more reasonable.
The try … catch construct has two main blocks:
try and then catch,
Catch’s job is to handle the exception or error that will be caught.
And the job of try is where the error can come, that is, the main code will be in try.
If you use try block, you will also need to use catch block. If an error is caught in the try block, it will handle the catch block.
If no error is caught, the catch block will not do anything, just ignore the code of the try block
alert( ‘’hi” );
alert(“inside catch block”);