简体   繁体   English

最后使用try .. catch ..进行Javascript错误处理

[英]Javascript error handling with try .. catch .. finally

I have a suspicion that I'm using the finally block incorrectly, and that I don't understand the fundamentals of its purpose... 我怀疑我正在使用finally块,并且我不理解其目的的基本原理......

 function myFunc() {
      try {
           if (true) {
                throw "An error";
           }
      } catch (e) {
           alert (e);
           return false;
      } finally {
           return true;
      }
 }

This function will run the catch block, alert "An error", but then return true. 此函数将运行catch块,警告“An error”,但随后返回true。 Why doesn't it return false? 为什么不返回虚假?

The finally block contains statements to execute after the try and catch blocks execute but before the statements following the try...catch statement. finally块包含在try和catch块执行之后但在try ... catch语句之后的语句之前执行的语句。 The finally block executes whether or not an exception is thrown. finally块执行是否抛出异常。 If an exception is thrown, the statements in the finally block execute even if no catch block handles the exception. 如果抛出异常,即使没有catch块处理异常,finally块中的语句也会执行。 more 更多

The finally block will always run, try returning true after your try block finally块将始终运行,尝试在try块后返回true

function myFunc() {
     try {
         if (true) {
               throw "An error";
          }
          return true;
     } catch (e) {
          alert (e);
          return false;
     } finally {
          //do cleanup, etc here
     }
 }

Finally blocks execute when you leave the try block. 离开try块时,最后执行块。 In your code this happens when you return false. 在你的代码中,这会在你返回false时发生。 That sets the return value to false and attempts to exit the function. 这会将返回值设置为false并尝试退出该函数。 But first it has to exit the try block which triggers the finally and overwrites the return value to true. 但首先它必须退出触发finally的try块并将返回值覆盖为true。

It is considered by many to be a good programming practice to have a single return statement per function. 许多人认为每个函数都有一个返回语句是一个很好的编程习惯。 Consider making a var retval at the beginning of your function and setting it to true or false as appropriate throughout your function and then structuring the code so that it falls correctly through to a single return at the bottom. 考虑在函数的开头创建一个var retval,并在整个函数中将其设置为true或false,然后构造代码,使其正确地落到底部的单个返回。

function getTheFinallyBlockPoint(someValue) {
    var result;
    try {
        if (someValue === 1) {
            throw new Error("Don't you know that '1' is not an option here?");
        }
        result = someValue
    } catch (e) {
        console.log(e.toString());
        throw e;
    } finally {
        console.log("I'll write this no matter what!!!");
    }

    return result;
};

getTheFinallyBlockPoint("I wrote this only because 'someValue' was not 1!!!");
getTheFinallyBlockPoint(1);

Run this on your browser's console and it might give you the answer you're looking for. 在浏览器的控制台上运行它,它可能会给你你正在寻找的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM