首页 > javascript > 我在Javascript中使用以下try,throw和catch异常处理程序做错了什么?

我在Javascript中使用以下try,throw和catch异常处理程序做错了什么? (What am I doing wrong with the following try, throw and catch exception handlers in Javascript?)

2012-06-29 javascript

问题

我有以下代码,但不使用throw,但是当我使用throw关键字时,它不会返回相应的消息。我究竟做错了什么?

更新

我故意调用该函数addme,而不是addMe因为我想捕获错误消息。

代码 - 无需使用即可使用 throw

function addMe() {
        var a = 1;
        var b = 2;
        return a+b;
    }

    try {
        addme();
    }


    catch (err) {
        document.write(err.name + " " + err.message);
    }

代码 - 这不起作用

function addMe() {
        var a = 1;
        var b = 2;
        return a+b;
    }

    try {
        addme();
        throw "error 1";
    }

    catch(err) {
        if(err ==  "error 1") {
            document.write("This is a custom message for error 1");
        }
    }

解决方法

addme是未定义的,所以你永远不会达到你的throw陈述。(具体来说,ReferenceError当你打电话时,首先抛出a addme而不是addMe)

要记住的关键是从上到下读取程序 - 直到你注入一个GOTO(一个错误,调用一个函数,从一个函数返回等),然后将它发送到其他地方。一旦你GOTO Raptor不能保证在那条线下面的线被调用。

try {
    addme(); // Reference Error Thrown - go to catch statement
    throw "error 1"; // We never get here
}

catch(err) {
    // Never true - err is always a ReferenceError.
    if(err ==  "error 1") {
        document.write("This is a custom message for error 1");
    }

要处理任何类型的错误,您可以检查err instanceof TYPE_OF_ERROR

catch(err) {
    if(err instanceof ReferenceError) {
        document.write("This is a custom message for ReferenceErrors");
    }
}

(请记住,字符串不是错误)

问题

I have the following code that works without using throw however when I use the throw keyword it doesn't return the appropriate message. What am I doing wrong?

Update

I am deliberately calling the function addme as opposed to addMe as I want to catch the error message.

Code - That works without using throw

function addMe() {
        var a = 1;
        var b = 2;
        return a+b;
    }

    try {
        addme();
    }


    catch (err) {
        document.write(err.name + " " + err.message);
    }

Code - That does not work

function addMe() {
        var a = 1;
        var b = 2;
        return a+b;
    }

    try {
        addme();
        throw "error 1";
    }

    catch(err) {
        if(err ==  "error 1") {
            document.write("This is a custom message for error 1");
        }
    }

解决方法

addme is undefined, so you never reach your throw statement. (Specifically, a ReferenceError is thrown first when you call addme rather than addMe)

The key thing to remember is the program is read from the top down - until you inject a GOTO (an error, calling a function, returning from a function, etc.) that sends you somewhere else. Once you GOTO Raptor the lines beneath that line are not guaranteed to be called.

try {
    addme(); // Reference Error Thrown - go to catch statement
    throw "error 1"; // We never get here
}

catch(err) {
    // Never true - err is always a ReferenceError.
    if(err ==  "error 1") {
        document.write("This is a custom message for error 1");
    }

To deal with any type of error you can check err instanceof TYPE_OF_ERROR:

catch(err) {
    if(err instanceof ReferenceError) {
        document.write("This is a custom message for ReferenceErrors");
    }
}

(And please remember, a string is not an error)

相似信息