0

どうすればエラーを見つけることができますか

try {
    undef
} catch (e){
    console.log(e)
    console.dir(e)
}

console.log (firebug 内) に次の情報が含まれているため、情報はどこかにありません。

ReferenceError: undef is not defined

しかし、eオブジェクトをブラウズしても見つかりません。

エラーが何であるかをプログラムで見つけて、それに応じてエラーを処理するにはどうすればよいですか?

編集: エラーオブジェクトを示すスクリーンショット

4

3 に答える 3

1
try {

    if(typeof undef  == 'undefined'){       
        console.log('We should not access this "undef" var');
    }       
    console.log('The next line will produce an exception');
    undef
} catch (e){
    console.log(e);
    for(index in e){
        console.log(index+' ('+(typeof e[index])+'): '+ e[index]);
    }
}

どちらが生成されますか:


この「undef」var にアクセスするべきではありません
次の行は例外を生成します
ReferenceError: undef が定義されていません
ファイル名 (文字列): file:///B:/xampp/htdocs/study/test.html
lineNumber (数): 12
スタック (文字列): @file:///B:/xampp/htdocs/study/test.html:12

于 2012-08-10T19:31:46.643 に答える
0

その型を明示的にプルできるとは思いませんが、テストすることはできます。

try {
    undef
} catch (e){
    console.log(e)
    console.dir(e)
    if(e instanceof ReferenceError) {
      console.log("Ooops! Fat fingers!");
    }
}
于 2012-08-10T19:20:05.523 に答える
0

しかし、試行錯誤の結果、これが得られます...

try {
    undef
} catch (e){
    console.log(e.toString())
    // ReferenceError: undef is not defined
}

私はfirebugがの.toString()メソッドにアクセスしているだけだと思いますe


編集:

このメソッドは、クロス ブラウザーで.toString()保証されている 2 つのプロパティのみを連結しているだけだと推測しています。namemessagee.message

于 2012-08-10T19:26:09.317 に答える