Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I throw an exception in Javascript, that stops Javascript execution?

Tags:

javascript

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.

like image 598
Mathias F Avatar asked Feb 24 '12 13:02

Mathias F


People also ask

Do exceptions stop execution?

Yes, uncaught exceptions result in fatal errors that stop the execution of the script.

Can you throw exceptions in JavaScript?

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.

Does JavaScript continue execution after error?

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.


Video Answer


4 Answers

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)

like image 83
Rob W Avatar answered Sep 28 '22 08:09

Rob W


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.

like image 36
Mouna Cheikhna Avatar answered Sep 28 '22 08:09

Mouna Cheikhna


Use try catch finally block

It will do the trick

like image 38
Kunal Vashist Avatar answered Sep 28 '22 06:09

Kunal Vashist


Stop the execution with

throw new Error('stopIt');

This will also do the trick:

throw 'Stop It!';
like image 23
Imbro Avatar answered Sep 28 '22 07:09

Imbro