Programmers always face errors in their script. That can happen for various reasons. “try…catch” method handles the errors written in code.
Usually there are three blocks. These are “try, catch & finally”.
Try Block: This block is used for writing the error in the code.
Catch Block: This block is used for finding or catching the error.
Finally Block: This block returns important statement like closing the connection or something like this & this block executes even the try block has any error.
First the try block does it’s work. If it doesn’t find any error in the try area then it executes the code without going to the catch block. And if it finds any error, it even ignores the rest of the try block and executes the catch block with the information of that error.
The work of try catch finally block can be done in two methods. At first, if there is no error, then the try & finally block works. And if there is errors in the code, the block works as first ‘try’ ,then ‘catch’ and lastly ‘finally’.
Coding Syntax in JS:
- There must be a space between parameters.
- There should be spaces around operators.
- A semicolon should be used after each statement if it’s mandatory or not.
- Lines should not be very long.
- There should be empty line between two blocks of code.
There must be spaces used before and after of curly braces. Curly braces are normally written in Egyptian style as the first brace stays in the function declaration line. But if not needed curly brackets should not be used in extra. A line without curly brackets is acceptable if the line is short enough.
There are two types of comment. Single line comment and multi-line comment. Single line comments in code starts with ‘//’ (double forward slash symbol). And double or multi-line comments starts with “/*” and ends with “*/” symbol. The comments should be written in good style for the programmers to understand the comments clearly. If not necessary comments should be avoided and programmers should try to write reasonable comments and fully avoid writing bad comments.
If any other error occurs in the try area of the code, it is important to catch that. For that reason Rethrowing is used. It is also very important term in try…catch syntax.
Indents in JS:
There are two types of indentations. One is horizontal and another is vertical. The horizontal indentation is used usually pressing the tab key where it occurs 2 or 4 spaces and it is a good practice. The vertical indentations are always should be made in conventional form for the code to look readable for any other programmer.
The length of a line should not be very long because that is not suitable for looking the code good and also it sometimes hampers the flow of code. So, line gaps should be make reasonably and for that there are also some shortcuts available now in various code editors.
Nesting level mustn’t be many levels long. For that you can use loops such as for loop, while loop, do…while loop etc. The format of the nested levels must be well enough for other programmers to understand.