The try...catch statement allows you to test a block of code for errors.
is a runtime error and asking "Do you wish to debug?". Error message like this may be
useful for developers but not for users. When users see errors, they often leave the Web
The try...catch statement allows you to test a block of code for errors. The try block contains
the code to be run, and the catch block contains the code to be executed if an error occurs.
Note that try...catch is written in lowercase letters. Using uppercase letters will generate a
The example below is supposed to alert "Welcome guest!" when the button is clicked.
However, there's a typo in the message() function. alert() is misspelled as adddlert(). A
it. The code displays a custom error message informing the user what happened:
The next example uses a confirm box to display a custom message telling users they can click OK
to continue viewing the page or click Cancel to go to the homepage. If the confirm method
returns false, the user clicked Cancel, and the code redirects the user. If the confirm method
returns true, the code does nothing:
The throw statement can be used together with the try...catch statement, to create an exception
for the error. Learn about the throw statement in the next chapter.
Your Query was successfully sent!