Why does an error code not execute the parts without error?

Asked

Viewed 92 times

0

console.log(a);
console.log('still going...');

The above code is an example of MDN Web Docs, the above example tries to show the variable value a, but the variable a was not defined, thus an error is returned in the console browser:

Uncaught ReferenceError: a is not defined
at <anonymous>:1:13

Only the problem is that the code below:

console.log('still going...');

Has no type of error because it was not executed?

  • 4

    Test this for you to see window.onload = function() {&#xA; document.body.style.background = 'red';&#xA; console.log(a);&#xA;};&#xA;&#xA;console.log('still going...'); Puts error within a scope...

  • So it’s not a rule that some code error stops working the rest @hugocsl.? mainly on Javascript.

  • 2

    @Virgilionovic depends on the situation, I gave the example only didactically, if the error is within one function it does not interfere in another. The way AP asked is very generic... Within a single scope everything that comes below the error usually does not perform. Without a real situation it’s hard to say what, so I left it as a comment. The fact that the error occurs in an insolated place within a function may not disturb the rest of the code, depending on the case logically... And I don’t even know about JS, but I’ve seen a lot of code with "working" rss error

  • 1

    @hugocsl this, I also agree with what you did, it is not always, it is not rule, mainly in the box of surprise Javascript. His question really is that it stops working, but perhaps (as the staff does not ask a better question that encompasses more scenarios) is his example a good omen in saying that everything depends. Important what you did.

  • @hugocsl, that was it, it worked! without having to stop all the code because of an error, please be as you add a reply with more details?

  • @hugocsl, this means that if the error is within the scope of the function a(), this same error will not affect the function b() that is out of function a()???

  • Draw the JS is not my specialty, I prefer to let someone with more experience answer to you, that if it appears here someone willing... Maybe if you ask a more objective question and with your real problem, with the real code of the problem, someone can help you more closely to your case... An error in the a() function will not necessarily block b(), but you have to expose your actual situation with your current code

  • Okay, but thank you so much for answering, it opened up another way for me to snoop.

Show 3 more comments

1 answer

3


In this context, at the moment that there is an error in the code it does not have because it keeps running, everything else is compromised. It may be that some cases are not, but it is difficult to assess this, if it was wrong is always treated as impossible to continue, even if you can do it properly. In theory I could create a way to evaluate this and continue, but it gives a monumental work, slows down and does not bring practical advantages. Fix the mistake and be happy.

As noted above there is context that the execution does not stop, but the error still exists. It is Javascript being Javascript (inconsistency). I did tests in various situations (within the scope as per the comment) and in all the code stops running, hugocls showed a case that can be considered corner case who agrees to continue. Unless someone presents a rationale of the committee, or something in the specification saying that they wanted this, I consider that it is either error in specification or implementation (very common even because JS have one of the worst specifications ever created giving room for much interpretation of the implementer - curiously in contrast to C that lets the implementer have a lot of flexibility without being vague or ambiguous)even indicates this because it does not make much sense not only to continue, but to continue in such a specific case.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.