search

Home  >  Q&A  >  body text

javascript - Questions about try, catch, throw?

try{
    var num = pompt("请输入5~10");
}
catch(err){
    console.log(err);
    console.log(err + "这里我随便加上一段字符串");
}

In the above code, console.log(err); is output on the console like this: ReferenceError: pompt is not defined at index.html:37, there is a prompt line Number.
Butconsole.log(err "I will add a random string here");The output in the console is like this: ReferenceError: pompt is not definedHere I will add a random string String , if a string is added, no error line number will be prompted. What is the reason for this?

PHP中文网PHP中文网2754 days ago523

reply all(4)I'll reply

  • PHP中文网

    PHP中文网2017-05-19 10:33:16

    The first sentence prints an object.

    The second sentence prints a string. That is, err.toString() + "I just add a string here"

    try{
        var num = pompt("请输入5~10");
    }
    catch(err){
        console.log(err);
        console.log(err.toString());
    }
    

    Non-professional answer, for reference only-. -

    reply
    0
  • 漂亮男人

    漂亮男人2017-05-19 10:33:16

    You will know after logging err.toString()

    Because err+str, err first calls toString to convert it into a string.

    reply
    0
  • PHP中文网

    PHP中文网2017-05-19 10:33:16

    try{
        var num = pompt("请输入5~10");
    }
    catch(err){
        console.log(err);
        console.log(err , "这里我随便加上一段字符串");
    } 

    It can be like this

    reply
    0
  • 怪我咯

    怪我咯2017-05-19 10:33:16

    err is data in the form of an object, and is automatically converted into a string when connected with a plus sign.

    reply
    0
  • Cancelreply