There are three types of programming errors: (1) syntax errors and (2) run-time error (3) logic errors:
For example, the following line will result in a syntax error because it lacks a closing parenthesis:
Execution (after compilation/interpretation) at run-time errors, also known as exceptions, is raised.
For example, the following line will cause a run-time error because the syntax here is correct, but at run time it is trying to invoke a non-existent method:
Logic errors can be the most difficult type of error tracing. These errors are not a result of a syntax or run-time error. Conversely, when an erroneous driver script logic occurs, you do not get the desired result.
You may not be able to catch these errors because it depends on what type of logic the program is based on business requirements.
Here is the try...catch...finally block syntax:
The try block must be followed by only one catch block or one finally block (or both). When an exception is placed in the try block, it is executed in addition to the E and catch blocks. The optional finally block after the Try/catch statement executes unconditionally.
Here is an example of an attempt to invoke a nonexistent function that throws an exception. Let's take a look at its behavior, not having a try ... catch:
Now, let's try to use the try ... catch to catch this exception and display a user-friendly message. You can also cancel this message if you want to hide this error from the user.
You can use the finally block to execute unconditionally after the statement is Try/catch forever. Here is an example:
You can use the throw statement to improve your built-in or custom exceptions. These anomalies can then be captured and the appropriate action can be taken.
The following is an example of the use of a throw statement.
You can use strings, integers, booleans or objects to throw an exception in a function, then you can catch exceptions in the same function that we did above, or use the Try ... catch block in other functions.
OnError () syntax
The OnError event handler provides three information to identify the exact nature of the error:
- The error message. The browser will display the same message as the given error
- Url. In the file where the error occurred
- Line number. The line number given in the URL that caused the error
Here is an example to illustrate how to extract this information
Can be displayed in any way that you think it is better to extract information.
You can use the OnError method to display error messages in cases where there are no problems loading images as follows:
You can use the onerror to display the appropriate information in the case of many HTML tags incorrectly.
You may be interested in
- 10 Advanced tips for debugging with the console
- Where there's a browser, there's fundebug.
- Debug Front End Html/css