![]() |
VOOZH | about |
JavaScript uses throw to create custom errors and try...catch to handle them, preventing the program from crashing. The finally block ensures that code runs after error handling, regardless of success or failure.
we make use of 'dadalert' which is not a reserved keyword and is neither defined hence we get the error.
The try statement allows you to check whether a specific block of code contains an error or not. The catch statement allows you to display the error if any are found in the try block.
try {
Try Block to check for errors.
}
catch(err) {
Catch Block to display errors.
}
our catch block will not run as there's no error in the above code and hence we get the output 'Value of variable a is: 10'.
The throw statement allows you to create custom error messages and throw exceptions manually.
throw new Error("Error message");throw creates a custom error.Error.The finally Statement runs unconditionally after the execution of the try/catch block. Its syntax is
Output: The Finally Block can also override the message of the catch block so be careful while using it.
You can create custom error types by extending the built-in Error class. This can be useful for more specific error handling.