473,288 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,288 software developers and data experts.

javascript cause itself to stop ?



My javascript program has reason to want to stop.

For example, function A has 5 lines, the second of which calls function
B, which has 5 lines, the first of which calls function C.

But function C discovers that something is very wrong so it does an
"alert" saying something like

Sorry, couldn't make the necessary connection

At this point, I want the program to STOP rather than returning to
function B.

What can I put in function C after the "alert" to make it STOP completely ?

It's too cumbersome to have to put a special check into function B to
make sure that function C was able to make the required connection (not
to mention function C having to communicate this to function B), plus
having to then also put similar verification into function A etc etc.

It's much easier if I can have functions A and B *assume* that function
C successfully made the connection, and in turn function C causes a real
STOP if it can't make the connection.

In case you're wondering how global a STOP I'm talking about, since
clearly I don't want the entire web browser to stop, let alone the
entire machine (ha ha), notice what happens when a syntax error is
encountered in the middle of a javascript. The browser manages to STOP
the javascript after displaying the error. It's that kind of a STOP I'm
looking for.

(in fact, I was tempted to implement my STOP in a very ugly way, by
intentionally doing some sort of "eval" of a syntactically bogus string,
but I didn't try this yet)

er*******@rcn.com 2-Mar-2004

Jul 20 '05 #1
8 2001
Lee
Eric Osman said:

My javascript program has reason to want to stop.

For example, function A has 5 lines, the second of which calls function
B, which has 5 lines, the first of which calls function C.

But function C discovers that something is very wrong so it does an
"alert" saying something like

Sorry, couldn't make the necessary connection

At this point, I want the program to STOP rather than returning to
function B.

What can I put in function C after the "alert" to make it STOP completely ?

It's too cumbersome to have to put a special check into function B to
make sure that function C was able to make the required connection (not
to mention function C having to communicate this to function B), plus
having to then also put similar verification into function A etc etc.
That's how professionals generally write code. Function C should return
a status code (possibly just true or false) indicating success.

If you're sure that your users have modern browsers, you could also
look into "throw"-ing an exception, but that could be even more
trouble than returning a status code, if you also plan to "catch"
the exception.
(in fact, I was tempted to implement my STOP in a very ugly way, by
intentionally doing some sort of "eval" of a syntactically bogus string,
but I didn't try this yet)


Note that this would disable all Javascript in the page, preventing them
from just trying again, etc.

I once evaluated commercial code that responded to any error condition
by calling an "abort" function that simply divided by zero. We didn't
buy it.

Jul 20 '05 #2
>>It's too cumbersome to have to put a special check into function B to
make sure that function C was able to make the required connection (not
to mention function C having to communicate this to function B), plus
having to then also put similar verification into function A etc etc.

That's how professionals generally write code. Function C should return
a status code (possibly just true or false) indicating success.

Let me put it another way then.

Suppose function A calls function B calls function C .

In fact, fuction A calls functions D, E, and F, all of which also call C.

In the original version of function C, it returns a string. It was
never believed to ever fail.

Now, in version 2, we realize that there is a condition under which
function C can't return its string , and we do not intend to provide
recovery for this situation. We only want to put up a message that says

Sorry, please try again during business hours

and then stop.

If we have to redesign function C to return a status code, then we have
to return the string elsewhere. Sure, there are ways to do this, like
return an object that contains both a status code and a string.

But look at all the extra stuff we have to do ! We have to recode
functions A, B, D, E, and F .

Such a recode could be considered warranted if we're enhancing the
product to include some sort of recovery .

But in our example, we don't intend to do any kind of recovery , so the
recoding of all those routines would look something like this:

old code:

function B() {
var str = C();
do stuff
}

new code:

function B() {
var obj = C();
var str;
if (obj.status) {
str = obj.str;
do stuff
}
}
What a pain. I really do think this would be an example in which a
"stop" routine (to be called from function C) would be appropriate
instead of requiring all the recoding.

er*******@rcn.com

Jul 20 '05 #3
Eric Osman <er*******@rcn.com> writes:
What a pain. I really do think this would be an example in which a
"stop" routine (to be called from function C) would be appropriate
instead of requiring all the recoding.


That's what exceptions are for! Nobody will add a "stop" function
when they can just do:

throw "Try again during business hours!";

and even add
try {
... B() ... D() ... E() ... F() ....
} catch (error) {
alert(error);
}
around the body of function A.

The browsers that doesn't support "throw" (IE 4, NS 4, or
contemporaries) are unlikely to be extended with a "stop" function
anyway.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4
Lee
Eric Osman said:
It's too cumbersome to have to put a special check into function B to
make sure that function C was able to make the required connection (not
to mention function C having to communicate this to function B), plus
having to then also put similar verification into function A etc etc.

That's how professionals generally write code. Function C should return
a status code (possibly just true or false) indicating success.

If we have to redesign function C to return a status code, then we have
to return the string elsewhere. Sure, there are ways to do this, like
return an object that contains both a status code and a string.


Not at all. On failure, you have it return a special string
that can't be confused with a valid return value, typically
an empty string.

Jul 20 '05 #5


Lee wrote:
Eric Osman said:
It's too cumbersome to have to put a special check into function B to
make sure that function C was able to make the required connection (not
to mention function C having to communicate this to function B), plus
having to then also put similar verification into function A etc etc.
That's how professionals generally write code. Function C should return
a status code (possibly just true or false) indicating success.

If we have to redesign function C to return a status code, then we have
to return the string elsewhere. Sure, there are ways to do this, like
return an object that contains both a status code and a string.

Not at all. On failure, you have it return a special string
that can't be confused with a valid return value, typically
an empty string.


Not sufficient. Routine C could be modified to return an empty string,
but then routine B needs to be taught to check for it, and then routine
B has to be redesigned (we didn't even discuss what routine B was
returning) and then all the calls to routine B have to be redesigned.

In other cases the situation could be even different. For example,
consider a routine C that is supposed to allocate a global array of
100000 elements, something like

for (var i=0; i<100000; i++)
someGlobalArray[i] = ...

This routine C doesn't return anything, and hence all the callers to it
don't check for any return value.

Similarly, in this new example, let's say routine B doesn't return any
value either, so none of the calls to B ever check for one.
Let's say the system is low on memory, so that somewhere during that
"for" loop, javascript can't expand the array.

Presumably, javascript puts up some sort of "out of memory" error box in
this case, and I'd bet javascript knows how to STOP rather than allowing
routine C to continue running.

So I'm merely asking how one would implement a similar kind of STOP to
those that javascript presumably already knows how to do.

Hmmm, is there a "throw" in javascript ? Maybe some condition can be
"thrown" to cause the stop ?

Jul 20 '05 #6
Lee <RE**************@cox.net> wrote in message news:<c2********@drn.newsguy.com>...
Not at all. On failure, you have it return a special string
that can't be confused with a valid return value, typically
an empty string.


Or return null. If returning null already has a special meaning to
existing code, then would I suggest a special string. Of course this
still requires editing existing code, which is just how things work.
There is no way to stop execution of a thread, short of causing a
javascript error. This is a very hacky way of doing it because you
have to cause a specific error and then assume any time that error
occurs, it is because you caused it on purpose. If the error occurs as
a genuine error, it will be thrown away. Check out my code below. The
onerror handler is set right before the error is caused, and then
removed right away. It is possible another thread could cause this
same error within this time, but if we are going to implement a hack,
we should make it least likely as possible that it could malfunction.
This code is IE 5.5+ specific, but is just meant as an example anyway.
Try it out...

alert( "before" );
stop();
alert( "after" );

function stop () {
window.onerror = handleError;
new Number().toExponential( -1 );
}

function handleError ( errorMessage ) {
window.onerror = null;
if ( errorMessage == "The number of fractional digits is out of
range" ) return true;
}
Jul 20 '05 #7
Eric Osman wrote:
Hmmm, is there a "throw" in javascript ? Maybe some condition can be
"thrown" to cause the stop ?


If you can guarantee a javascript version that supports exceptions in
the users environment then go ahead. I write a lot of server-side
"Javascript" so I know I can make such a guarantee, here's a contrived
example (not tested, but you get the idea!):

function DoSomething(intParam1, strParam2) {
try {
if (isNaN(intParam1)) {
throw new Error(-1, "\"intParam1\" is not a number");
}
if ((typeof strParam2) != "string" || strParam2.length == 0) {
throw new Error(-2, "\"strParam2\" is an invalid or null
string");
}

return "example";
}
catch (err) {
throw new Error(err.number, "DoSomething() failed with
parameters intParam1=\"" + intParam1 + "\", strParam2=\"" + strParam2 +
"\". Message=\r\n" + err.description);
}
finally {
ClearUp();
}
}

function Main() {
try {
alert(DoSomething(1, "test"));
}
catch (err) {
alert("Oops, an error occurred:\r\n" + err.description);
}
}

Main();
--
Andrew Urquhart
- FAQ: http://jibbering.com/faq
- Archive: http://groups.google.com/groups?grou...ang.javascript
- Reply: http://www.andrewu.co.uk/about/conta...ject=Re%3A+clj
Jul 20 '05 #8
Lee
Eric Osman said:

Lee wrote:
Eric Osman said:
>It's too cumbersome to have to put a special check into function B to
>make sure that function C was able to make the required connection (not
>to mention function C having to communicate this to function B), plus
>having to then also put similar verification into function A etc etc.
That's how professionals generally write code. Function C should return
a status code (possibly just true or false) indicating success.
If we have to redesign function C to return a status code, then we have
to return the string elsewhere. Sure, there are ways to do this, like
return an object that contains both a status code and a string.

Not at all. On failure, you have it return a special string
that can't be confused with a valid return value, typically
an empty string.


Not sufficient. Routine C could be modified to return an empty string,
but then routine B needs to be taught to check for it, and then routine
B has to be redesigned (we didn't even discuss what routine B was
returning) and then all the calls to routine B have to be redesigned.


Yes. Welcome to professional programming. Note that there may be
some calls to B that don't need to check the status, and that, in
most cases, it should be better for a routine to return an empty
value than to simply fail. If you need to modify your B code to
handle the case of receiving an empty string, you might as well do
that now, rather than 3 months from now when you discover some
other case in which an empty string might be returned.
In other cases the situation could be even different. For example, ....This routine C doesn't return anything, and hence all the callers to it
don't check for any return value.
And so in that case, you return true or false, and modify the
callers that care to check the value. It's too bad that you
didn't realize the need for status checking earlier, but it
shouldn't be such a big deal to modify the calls.

Hmmm, is there a "throw" in javascript ? Maybe some condition can be
"thrown" to cause the stop ?


Throw was one of my suggestions in my original response,
but it's not supported in older browsers, and you'll still
want to modify the calling routines to catch the exception.

Jul 20 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Andy Fish | last post by:
Hi, I want to use an anchor tag to invoke some javascript and I've read that it's bad form to use <a href="javascript:foo()"> I've read endless usenet posts and hint sites on the net, they all...
3
by: Jason | last post by:
I have two applications that I wrote that use asp and javascript source files. Recently both of these applications starting acting strange on my test box. Image icons will randomly stop showing...
4
by: houstoncity2004 | last post by:
Hi, I need help to JavaScript. I am new to this, and am trying to the page online ASAP. I have two submit buttons on one form, and each button will have to do different things. I named both...
12
by: Mark Fox | last post by:
Hello, I am attempting to do something very simple. I have a page MainPage.aspx and a popup window Popup.aspx. When users click on the linkbutton in the popup window I am looking to do some...
4
by: Malik Arykov | last post by:
Hello, All! how debug embedded javascript?
4
by: Steve Murphy | last post by:
I'd like to package a navigation bar into a user control. The problem I'm having is getting the rollover buttons to work. Where do I place the Javascript for the image caching and switching for the...
7
by: dotnetprogrammer via DotNetMonster.com | last post by:
I have an NT Service written in c# (.net framwork 2.0). When it hits a certain error condition, I want it to be to cause itself to stop. How can I do this? -- Message posted via...
8
by: chrisdude911 | last post by:
how do i add video into a javascript web page with my own custom buttons?
3
by: Niall | last post by:
The short version: how does one get Javascript to abort a form submission/page load half way through? Long version: I have a page, the guts of which can be summarised as: <script>
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.