In JavaScript, we use the optional catch binding function to skip the error
argument of the catch block in a try...catch
statement.
// try...catch with error arguumenttry{} catch(err) { // error argument is present}// try...catch without error arguumenttry{} catch{ // error argument is skipped}
Note: This feature was added in the ES10/2019.
The error
argument contains the reason and cause of the exception.
The optional catch binding can be used when we need to handle the error but are not interested in the error's cause.
Let's use the optional catch binding for handing the JSON parse exception.
In the above code:
abc
as a JSON using the JSON.parse
method. However, this will throw an error because it is not a valid JSON string. RELATED TAGS
CONTRIBUTOR