22

I try to simulate a problem where a script that is loaded from an external url stops execution of any more scripts on my site.

I tried to simulate such a problem by calling a function that does not exits. I can see the error in firebug but different scripts on the page are still executed.

Are there different kinds of errors in Javascripts? If yes: what kind of error stops script execution? I only need this answer for Firefox.

EDIT

This question is easy to misunderstood but Rob W got it: I need to throw an exception and that exception needs to stop further script execution.

Mathias F
  • 15,906
  • 22
  • 89
  • 159
  • 1
    `throw new Error("Something bad happened.")` or http://www.w3schools.com/js/js_throw.asp or http://stackoverflow.com/questions/464359/custom-exceptions-in-javascript – mgraph Feb 24 '12 at 13:20

4 Answers4

26

Answer to the title: No
Answer to "Are there different kinds of errors in JavaScript**: Yes, see MDN: Error
Syntax errors will prevent a whole script block from being executed, other errors (TypeErrors, Reference errors) will only stop the execution after the occurrence of the error.

Different <script> blocks are executed separately. You cannot prevent the second block from execution by throwing an error in the first block (Demo: http://jsfiddle.net/WJCEN/).

<script>Example: Syntax error in this script.</script>
<script>console.log('Still executed.')</script>

Also, if an error is caught using try-catch (demo: http://jsfiddle.net/WJCEN/1/), then the error will not even stop the execution a whole block.

try {throw 'Code';}catch(e){}
console.log('Still executed');


There is no general one-fit-all method to stop all code from running. For individual scripts, you can use some tricks to prevent the code from running further.

Example 1 (demo): Temporary overwrite a method

1: <script>window._alert = alert;alert=null;</script>
2: <script>alert('Some code!');confirm('Not executing');</script>
3: <script>alert=_alert;delete window._alert;alert('Done!');</script>

This method is based on the fact that script 2 expects alert to be a function. We have rewritten alert to a non-function property (script 1). Script 2 throws a TypeError, and the second block is skipped.
We restore the original values in script 3.

Example 2 (demo): Define a constant method, which cannot be overwritten.

4. <script>Object.defineProperty(window, 'test',{value:null});</script>
5. <script>var test=function(){alert('Test');};test();alert('What?');</script>

This methods relies on the Object.defineProperty method, to effectively define a constant value. In strict mode, the var test declaration would throw a TypeError: "test is read-only".
When strict mode is not enables, a TypeError will be thrown at test(): "test is not a function" (because we defined test to be constant, in script 4).

Note: The last method is not working correctly with function declarations (see bug #115452, Chrome 17)

imtheman
  • 4,713
  • 1
  • 30
  • 30
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • I guess that if I reference an external script file that this counts a different script block. Am I right? – Mathias F Feb 24 '12 at 14:01
  • @MalcolmFrexner Yes. See my updated answer. There is no single solution for all cases, the right approach differs per case. – Rob W Feb 24 '12 at 14:28
2

you can use the error object which support the following two properties:

name: The name of the error.
message: A description of the error.

for example to stop execution you can use : throw new Error("myError");

Are there different kinds of errors in Javascripts?

Besides the generic Error constructor, there are six other core errors in JavaScript:

see here details on these errors.

Mouna Cheikhna
  • 38,870
  • 10
  • 48
  • 69
2

Use try catch finally block

It will do the trick

Kunal Vashist
  • 2,380
  • 6
  • 30
  • 59
1

Stop the execution with

throw new Error('stopIt');

This will also do the trick:

throw 'Stop It!';
Imbro
  • 479
  • 4
  • 4
  • But won't stop like what OP was looking for, errors will only stop the single script block from executing, take a look at the accepted answer – Zer0 Sep 07 '20 at 09:34