/    Sign up×
Community /Pin to ProfileBookmark

Is there a JS ‘abort’ or ‘die’ function?

In PERL there is a ‘die’ function that stops execution of a program if certain conditions are not met.
For example, a needed file is not found because it is miss typed.

Is there a similar function for javascript?
The code I would think would follow something like this:

[code]
var FileName = ”; // might have expected to enter something like ‘ExternalFile.js’ from a Querystring
if (FileName == ”) {
alert(‘Invalid Filename’);
AbortAndDie(); // ??? unknown command or function to stop script execution
}
… continue with rest of script execution.
[/code]

Maybe something like an HTML <noscript> tag that would inform user of the problem.

Is there anything like this available or can be created for JS?
?

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@DanInMANov 10.2010 — <i>
</i>var FileName = ''; // might have expected to enter something like 'ExternalFile.js' from a Querystring
if (FileName == '') {
alert('Invalid Filename');

}
else {
return false
}

is that sufficient?

or you could use this, but Im not familiar with it myself

<i>
</i>
var fruit = "apple";

switch (fruit)
{
case "apple" :
document.writeln("An " + fruit + " is red.");
break;
case "bananna" :
document.writeln("An " + fruit + " is yellow.");
break;
default :
document.writeln("What color is your fruit?");
break;
}
Copy linkTweet thisAlerts:
@JMRKERauthorNov 11.2010 — <i>
</i>var FileName = ''; // might have expected to enter something like 'ExternalFile.js' from a Querystring
if (FileName == '') {
alert('Invalid Filename');

}
else {
return false
}

is that sufficient?

or you could use this, but Im not familiar with it myself

<i>
</i>
var fruit = "apple";

switch (fruit)
{
case "apple" :
document.writeln("An " + fruit + " is red.");
break;
case "bananna" :
document.writeln("An " + fruit + " is yellow.");
break;
default :
document.writeln("What color is your fruit?");
break;
}
[/QUOTE]


No, neither of those will work.

The 'return' needs to return from some function call.


For the 'true' portion, you would get an alert and the script would continue.

For the 'false' part, there is no function that is being called from to return somewhere. You would just get an error.

The 'switch' sequence only breaks out of the multiple IF ... THEN conditions.

The break just allows you to continue after the switched condition block.

The continue action would just execute the rest of the script.

The goal is to stop the script from continuing to execute if a known error is discovered during the execution of the script.
Copy linkTweet thisAlerts:
@rnd_meNov 11.2010 — use throw or anon-functions w/returns.

firefox also has stop().
Copy linkTweet thisAlerts:
@DanInMANov 11.2010 — Oh. Perhaps catch would be better? http://www.w3schools.com/js/js_try_catch.asp Oh i was getting close. Throw? - http://www.w3schools.com/js/js_throw.asp
Copy linkTweet thisAlerts:
@JMRKERauthorNov 11.2010 — I took the example from the links in post #5, but it still does not stop the script on errors.

Regardless if an error on entry is created or not, the alert('Script Continues') is executed.

I'm trying to halt the script from executing further if an error is thrown or caught (throw..catch)
<i>
</i>&lt;html&gt;
&lt;body&gt;
&lt;script type="text/javascript"&gt;
// From: http://www.w3schools.com/JS/tryit.asp?filename=tryjs_throw

var x=prompt("Enter a number between 0 and 10:","");
try {
if(x&gt;10) { throw "Err1"; }
else if(x&lt;0) { throw "Err2"; }
else if(isNaN(x)) { throw "Err3"; }
}
catch(err) {
if(err=="Err1") { alert("Error! The value is too high"); }
if(err=="Err2") { alert("Error! The value is too low"); }
if(err=="Err3") { alert("Error! The value is not a number"); }
}

alert('Script Continues -- with or without error detection');

&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;


I'm not sure how to create the anon-functions suggested by 'rnd me' in post #4 either w/ or w/o returns.
Copy linkTweet thisAlerts:
@DanInMANov 11.2010 — [CODE]<script type="text/javascript">
// From: http://www.w3schools.com/JS/tryit.asp?filename=tryjs_throw

var x=prompt("Enter a number between 0 and 10:","");
try {
if(x>10) { throw "Err1"; }
else if(x<0) { throw "Err2"; }
else if(isNaN(x)) { throw "Err3"; }
}
catch(err) {
if(err=="Err1") { alert("Error! The value is too high"); [B]break;[/B] }
if(err=="Err2") { alert("Error! The value is too low"); [B]break;[/B] }
if(err=="Err3") { alert("Error! The value is not a number"); [B]break;[/B] }
}

alert('Script Continues -- with or without error detection');

</script>[/CODE]


using break seems to stop execution.
Copy linkTweet thisAlerts:
@JMRKERauthorNov 11.2010 — What is post #7 about?

It looks the same as my post #6 and it does not contain any discussion?

?
Copy linkTweet thisAlerts:
@mrhooNov 11.2010 — If you want to break out of the code, [B]return [/B]from the catch.

[CODE](function (){
var x= prompt("Enter a number between 0 and 10:","");
try{
if(isNaN(x) || x> 10 || x<0) throw new Error(x);

}
catch(err){
if(x> 10) alert("Error! The value is too high");
else if(x<0) alert("Error! The value is too low");
else if(isNaN(x)) alert("Error! The value is not a number");
return;
}
alert('Script Continues -- with or without error detection');
})()[/CODE]


Of course, you can return from an alert without throwing the error, just as well.
Copy linkTweet thisAlerts:
@rnd_meNov 11.2010 — 
I'm not sure how to create the anon-functions suggested by 'rnd me' in post #4 either w/ or w/o returns.[/QUOTE]


[CODE]

(function(){ //anon fn wrap (provides return as breakpoint)
var FileName = ''; // might have expected to enter something like 'ExternalFile.js' from a Querystring
if (FileName == '') {
alert('Invalid Filename');

return; // ??? unknown command or function to stop script execution
}

alert("ok, cool");
//do more stuff



}());//end anon wrap;

//... continue with rest of script execution.[/CODE]




side note: an uncaught throw should halt execution of the current script file or "program".

[B][COLOR="Red"]edit:[/COLOR][/B]

doh! mrhoo's is about the same...

in that case, here's a new one that avoids function overhead and potentially expensive closures, yet provides the same break-out functionality you asked for:

[CODE]

while(1){
var FileName = ''; // might have expected to enter something like 'ExternalFile.js' from a Querystring
if (FileName == '') {
alert('Invalid Filename');

break; //AbortAndDie(); // ??? unknown command or function to stop script execution
}
//... continue with rest of script execution.


break; //keep this as last line
}[/CODE]
Copy linkTweet thisAlerts:
@KorNov 11.2010 — What is so much to discuss about?

[B]break[/B] - stops the loop and jumps out the loop, but continues to run the other codes outside the loop, if any

[B]return[/B] - stops unconditioned the code run

That is all.
Copy linkTweet thisAlerts:
@Sterling_IsfineNov 12.2010 — In PERL there is a 'die' function that stops execution of a program if certain conditions are not met.
[/QUOTE]
Such a facility just encourages sloppy coding practice. It's like using [I]break[/I] or an intermediate [I]return[/I] statement, it does the job but makes code harder to debug. If a condition is not met you just branch away from the rest of the code in the current function, setting the value of the [I]return[/I] statement ( which is always the last statement in the function ) to reflect why the function ended.
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...