JavaScript Editor Ajax toolkit     Ajax website 



Main Page

It is slow and potentially confusing to leave a finally block this way. Is this intentional?

A statement (either return or break) causes control of the program to leave the finally block. This may produce unintended consequences if there is a return or break statement in either of the try or catch blocks.

The code in a finally block is always run after the code in the try block and (if there is an error) after the code in the catch block. For example, if a return statement is encountered in a try block, the finally block is executed before the return statement is executed. If there is another return statement in the finally block, it will be executed, and the original return statement will not be executed. To avoid this potentially confusing situation, do not use a return statement in a finally block.

В CopyCode imageCopy Code
function test() {
   try {
      return(5);   // Attempt to return 5.
   } catch(e) {
      print(e);
   } finally {
      return(10);  // This gets run first, returning 10 instead of 5.
   }
}
print(test());     // Prints 10, not 5.

To correct this error

  1. Make sure that the return and break statements are not used in a finally block.

  2. Move the return or break statements to immediately follow the finally block if the intention is to execute them after the try and catch blocks.

See Also

Reference

try...catch...finally Statement

Concepts

Troubleshooting Your Scripts

Other Resources

JScript Reference



JavaScript Editor Ajax toolkit     Ajax website


©