The try/catch/finally Statement



Navigation Aids -- This Page           Navigation Aids -- This Topic           Navigation Aids -- This Site




Review of Terms Related to This Topic





Definition and Purpose

The try/catch statement is used to see if a certain statement or group of statements will throw an exception. The statements that need to be monitored to see if they will throw an error are kept in the try section. The catch section will execute the statements in it's block when a exception occurs in the statements of the try block. The finally block of statements will execute regardless of whether an error occurred.

The try/catch/finally statement is one of JavaScript exception handling tools. The purpose of these related statements is to provide an environment where statements (prone to runtime errors) can run and not hinder the general browser environment when an error occurs.

Why does the programmer need to catch an exception? Exceptions that are not caught will appear to the user as a runtime error. Not good!!





Statement Syntax


try {     •   statements to try     • } catch ( identifier ) {     •   catch statements     • } finally {     •   finally statements     • }



More on Syntax and Statement Behavior





ECMA-262 Section 12.14

The try statement is an ECMAScript core statement and is detailed is section 12.14 of the ECMAScript Language Specification. Here is a partial extraction of text from this section.

The try statement encloses a block of code in which an exceptional condition can occur, such as a runtime error or a throw statement. The catch clause provides the exception handling code. When a catch clause catches an exception, its identifier is bound to the exception.



Top            

Rx4AJAX        About Us | Topic Index | Contact Us | Privacy Policy | 2008 This Site Built By PPThompson