What is the difference between throw and alert

10

In the functions below I verify that the throw works the same as the alert. Can I replace it with an alert? Is there any inconvenience?

var letters = new Array("A","B","C","D","E")
function getLetter(fld) {
     try {
           var inp = parseInt(fld.value, 10)
               if (isNaN(inp)) {
                    throw "Entry was not a number."
               }
               if (inp < 0 || inp > 4) {
                   throw "Enter only 0 through 4."
               }
               fld.form.output.value = letters[inp]
     }
     catch (e)  {
               alert(e)
               fld.form.output.value = ""
               fld.focus()
               fld.select()
     }
}
<FORM>
Enter a number from 0 to 4: 
<INPUT TYPE="text" NAME="input" SIZE=5>
<INPUT TYPE="button" VALUE="Get Letter" onClick=getLetter(this.form.input)>
Matching Letter is:<INPUT TYPE="text" NAME="output" SIZE=5>
</FORM>

To better understand my doubt I make the same code with alert and it works the same way: to verify type a non-numeric in the input in both codes

var letters = new Array("A","B","C","D","E")
function getLetter(fld) {
     try {
           var inp = parseInt(fld.value, 10)
               if (isNaN(inp)) {
                    alert("Entry was not a number.")
               }
               if (inp < 0 || inp > 4) {
                   throw "Enter only 0 through 4."
               }
               fld.form.output.value = letters[inp]
     }
     catch (e)  {
               alert(e)
               fld.form.output.value = ""
               fld.focus()
               fld.select()
     }
}
<FORM>
Enter a number from 0 to 4: 
<INPUT TYPE="text" NAME="input" SIZE=5>
<INPUT TYPE="button" VALUE="Get Letter" onClick=getLetter(this.form.input)>
Matching Letter is:<INPUT TYPE="text" NAME="output" SIZE=5>
</FORM>
    
asked by anonymous 25.09.2018 / 02:52

3 answers

13

It just happens to have the same behavior in this particular code. In fact this is not even mandatory, and in fact JavaScript not used in a browser will change for sure. There is zero relationship between them.

alert() is a function present in the browser to alert you of something and is its only capacity. It is something from the library and has no major consequences on the code execution.

throw is something of the language, strongly interferes with the execution of the code and deviates its execution. He remembers a return because he ends the execution there and will stop at the first catch he finds. It is a flow control command, but it should not be used for this. It is useful in some cases to report error to another unknown part of the code. When properly used it can make the code easier to write and handle, if abused it can get much worse. He is a goto glorified, therefore very bad, unless he has a strong motive for his use that compensates for the problem he brings.

Almost always having a throw directly where it has a catch is an error. So in this case it does not make sense to do this, it's clearly a case to resolve in if .

I understand that this was just an example, but since it is in doubt of its use remember that it is useless in real code.

And to tell you the truth, JS does not have much of a culture to throw exceptions at, this turns out to be nice, because people find it cute and misuse it. There's a lot of material on the site about it.

See if I take out alert() of catch does not do the same thing.

var letters = new Array("A","B","C","D","E")
function getLetter(fld) {
    try {
        var inp = parseInt(fld.value, 10)
        if (isNaN(inp)) throw "Entry was not a number."
        if (inp < 0 || inp > 4) throw "Enter only 0 through 4."
        fld.form.output.value = letters[inp]
     } catch (e) {
        console.log("deu erro");
     }
}
<FORM>
Enter a number from 0 to 4: 
<INPUT TYPE="text" NAME="input" SIZE=5>
<INPUT TYPE="button" VALUE="Get Letter" onClick=getLetter(this.form.input)>
Matching Letter is:<INPUT TYPE="text" NAME="output" SIZE=5>
</FORM>

I placed GitHub for future reference .

And now without catch :

var letters = new Array("A","B","C","D","E")
function getLetter(fld) {
    var inp = parseInt(fld.value, 10)
    if (isNaN(inp)) throw "Entry was not a number."
    if (inp < 0 || inp > 4) throw "Enter only 0 through 4."
    fld.form.output.value = letters[inp]
}
<FORM>
Enter a number from 0 to 4: 
<INPUT TYPE="text" NAME="input" SIZE=5>
<INPUT TYPE="button" VALUE="Get Letter" onClick=getLetter(this.form.input)>
Matching Letter is:<INPUT TYPE="text" NAME="output" SIZE=5>
</FORM>

I placed GitHub for future reference .

So there's no way to establish any relationship between these two things.

As I anticipated it is not much used in JS and so has little material, but you can see about exceptions in other languages which is very similar:

25.09.2018 / 03:11
10

Actually the throw statement does not work equal to alert , which it does is throw a user-defined exception, and everything it has after throw will not run. And the control will be passed to the first block catch . In your case you have a catch block and in it you call the alert method and in it you pass the e parameter which is the exception you launched using throw .

If you go in your block catch and comment on the alert method you will see that no dialog box will open.

    
25.09.2018 / 03:10
7

Actually what happens is this:

  • throw throws an error
  • This error is caught by catch
  • o catch calls alert(e)

Example:

try {
    throw "erro!";
} catch(e) {
    alert(e);
}

% wont be displayed with error message.

But that does not mean that alert works equal to throw . Just remove alert from within alert to verify this:

try {
    throw "erro!";
} catch(e) {
    console.log(e);
}

Now the error message is displayed in catch , since I am no longer calling the console within alert (I changed by catch ).

Notice that no console.log was shown, because alert does not work equal to throw . It only throws the error (which will be caught by alert ).

Your second example is just calling a catch within alert . As no error is generated in this case, it does not enter try :

try {
    alert("Entry was not a number.")
} catch(e) {
    alert("Não serei executado");
}

In short, catch only throws the error, which will be caught by throw . In your first example, try removing catch that is within alert to see that no catch window will be displayed.

    
25.09.2018 / 03:12