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.
Yes, uncaught exceptions result in fatal errors that stop the execution of the script.
Technically you can throw an exception (throw an error). If you use throw together with try and catch , you can control program flow and generate custom error messages.
Typically, you don't want to continue execution when you manually throw a new error. If you want to have it for a logging purpose, you should consider an self-created internal solution. However, to catch a thrown error you need to invoke a try / catch statement.
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');
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.
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)
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.
Use
try catch finally block
It will do the trick
Stop the execution with
throw new Error('stopIt');
This will also do the trick:
throw 'Stop It!';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With