Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > sintax for try and except istruction

Reply
Thread Tools

sintax for try and except istruction

 
 
SAN CAZIANO
Guest
Posts: n/a
 
      10-09-2004
is there an istruction like the
try .....
except.....


 
Reply With Quote
 
 
 
 
VK
Guest
Posts: n/a
 
      10-09-2004
> try .....
> except.....


You ment the clause:
try {} catch (e) {} finally{}

Yes, it has been relatively recently added to both JavaScript and JScript.
Never seen it in use yet though in actual scripts. Either no much use, or
some implementation errors, or I don't know.
Would be great to get some feed-back from who ever used it.


 
Reply With Quote
 
 
 
 
Michael Winter
Guest
Posts: n/a
 
      10-09-2004
On Sat, 09 Oct 2004 10:44:35 GMT, SAN CAZIANO <> wrote:

> is there an istruction like the
> try .....
> except.....


var e;

try {

// Execute exception-causing code here

} catch(e) {

// e represents the exception thrown

} finally {

// Code always executed, whether an exception was thrown or not

}

A try statement doesn't require both of the catch and finally clauses, but
at least one of them must be included.

This is a fairly recent addition to the language. Browsers like NN4 will
error when encountering these keywords as they were reserved during that
time. IE5, Netscape 6 and Mozilla will support try..catch. I don't know
when Opera began support.

Are you certain you need to use these statements?

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
 
Reply With Quote
 
George Hester
Guest
Posts: n/a
 
      10-09-2004
One reason why it is used and in fact I use it for this reason.

When your page loads you cannot refer to an element's id until the element has actually been created. Now
consider you have made a page that is actually a template. The page dynamically loads an image. If the image is
large the <div id="oLImage...> loads but if the image is small <div id="oSImage ...> loads. You don't know
which image is going to be requested and therefore which div will be made. One or the other but not both will
be made. Here's the solution:

try{
// do something with element id='oLImage'
}
catch{
//do something with element id='oSImage'
}

No script errors on the page but in fact an error does occur when <div id='oSImage'....> (the image is
small) results which goes to the catch().

--
George Hester
__________________________________
"VK" <> wrote in message news:4167ce96$0$160$...
> > try .....
> > except.....

>
> You ment the clause:
> try {} catch (e) {} finally{}
>
> Yes, it has been relatively recently added to both JavaScript and JScript.
> Never seen it in use yet though in actual scripts. Either no much use, or
> some implementation errors, or I don't know.
> Would be great to get some feed-back from who ever used it.
>
>

 
Reply With Quote
 
George Hester
Guest
Posts: n/a
 
      10-09-2004
I'm sorry it should be:

try{
// do something with element id='oLImage'
}
catch(e){
//do something with element id='oSImage'
}

You can use the e to get the actual error message if you want:

try{
// do something with element id='oLImage'
}
catch(e){
var errMsg = 'The error was:\n\n';
errMsg += 'Error number: '+(e.number & 0xFFFF);
errMsg += '\n'+e.description;
//do something with element id='oSImage'
// alert(errMsg);
}

--
George Hester
__________________________________
"George Hester" <> wrote in message news:32S9d.144679$. ..
One reason why it is used and in fact I use it for this reason.

When your page loads you cannot refer to an element's id until the element has actually been created. Now
consider you have made a page that is actually a template. The page dynamically loads an image. If the image is
large the <div id="oLImage...> loads but if the image is small <div id="oSImage ...> loads. You don't know
which image is going to be requested and therefore which div will be made. One or the other but not both will
be made. Here's the solution:

try{
// do something with element id='oLImage'
}
catch{
//do something with element id='oSImage'
}

No script errors on the page but in fact an error does occur when <div id='oSImage'....> (the image is
small) results which goes to the catch().

--
George Hester
__________________________________
"VK" <> wrote in message news:4167ce96$0$160$...
> > try .....
> > except.....

>
> You ment the clause:
> try {} catch (e) {} finally{}
>
> Yes, it has been relatively recently added to both JavaScript and JScript.
> Never seen it in use yet though in actual scripts. Either no much use, or
> some implementation errors, or I don't know.
> Would be great to get some feed-back from who ever used it.
>
>

 
Reply With Quote
 
Michael Winter
Guest
Posts: n/a
 
      10-09-2004
On Sat, 09 Oct 2004 13:51:59 GMT, George Hester <>
wrote:

[snip]

> The page dynamically loads an image. If the image is large the <div
> id="oLImage...> loads but if the image is small
> <div id="oSImage ...> loads. You don't know which image is going to be
> requested and therefore which div will be made.


[snip]

> No script errors on the page but in fact an error does occur when <div
> id='oSImage'....> (the image is small) results which goes to the
> catch().


Assuming I understand you correctly, that isn't a valid reason to use
try..catch. Assuming you can't generate code that is relevant to the
generated mark-up, test. If the element doesn't exist, you won't get a
valid object reference. You simply need to test for that and act
accordingly. But as I said, generating the correct script for the current
mark-up is surely the sensible thing to do.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
 
Reply With Quote
 
George Hester
Guest
Posts: n/a
 
      10-09-2004
"Michael Winter" <> wrote in message newspsflw0iiix13kvk@atlantis...
> On Sat, 09 Oct 2004 13:51:59 GMT, George Hester <>
> wrote:
>
> [snip]
>
> > The page dynamically loads an image. If the image is large the <div
> > id="oLImage...> loads but if the image is small
> > <div id="oSImage ...> loads. You don't know which image is going to be
> > requested and therefore which div will be made.

>
> [snip]
>
> > No script errors on the page but in fact an error does occur when <div
> > id='oSImage'....> (the image is small) results which goes to the
> > catch().

>
> Assuming I understand you correctly, that isn't a valid reason to use
> try..catch. Assuming you can't generate code that is relevant to the
> generated mark-up, test. If the element doesn't exist, you won't get a
> valid object reference. You simply need to test for that and act


the test is try() the act accordingly is catch(e){}

> accordingly. But as I said, generating the correct script for the current
> mark-up is surely the sensible thing to do.
>
> Mike


>
> --
> Michael Winter
> Replace ".invalid" with ".uk" to reply by e-mail.


What's the problem with that?

George Hester
__________________________________

 
Reply With Quote
 
Michael Winter
Guest
Posts: n/a
 
      10-09-2004
On Sat, 09 Oct 2004 14:50:32 GMT, George Hester <>
wrote:

[snip]

>> Assuming I understand you correctly, that isn't a valid reason to use
>> try..catch. Assuming you can't generate code that is relevant to the
>> generated mark-up, test. If the element doesn't exist, you won't get a
>> valid object reference. You simply need to test for that and act
>> [accordingly.]

>
> the test is try() the act accordingly is catch(e){}


[snip]

> What's the problem with that?


It's unnecessary?

If you have an object reference,

if(objRef) {

will check if it's non-null. If at the time of that test, objRef
references one DIV that doesn't exist, the reference will be null and the
expression will evaluate as false.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
 
Reply With Quote
 
George Hester
Guest
Posts: n/a
 
      10-09-2004
"Michael Winter" <> wrote in message newspsfl01bhnx13kvk@atlantis...
> On Sat, 09 Oct 2004 14:50:32 GMT, George Hester <>
> wrote:
>
> [snip]
>
> >> Assuming I understand you correctly, that isn't a valid reason to use
> >> try..catch. Assuming you can't generate code that is relevant to the
> >> generated mark-up, test. If the element doesn't exist, you won't get a
> >> valid object reference. You simply need to test for that and act
> >> [accordingly.]

> >
> > the test is try() the act accordingly is catch(e){}

>
> [snip]
>
> > What's the problem with that?

>
> It's unnecessary?
>
> If you have an object reference,
>
> if(objRef) {
>
> will check if it's non-null. If at the time of that test, objRef
> references one DIV that doesn't exist, the reference will be null and the
> expression will evaluate as false.
>
> Mike
>
> --
> Michael Winter
> Replace ".invalid" with ".uk" to reply by e-mail.


six of one 1/2 dozen of the other

I never said it was necessary just sufficient for browsers which support it.

George Hester
__________________________________
 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      10-09-2004
George Hester wrote:
> Michael Winter wrote:
>> George Hester wrote:

<snip>
>>> No script errors on the page but in fact an error does occur
>>> when <div id='oSImage'....> (the image is small) results
>>> which goes to the catch().

>>
>> Assuming I understand you correctly, that isn't a valid reason
>> to use try..catch. Assuming you can't generate code that is
>> relevant to the generated mark-up, test. If the element doesn't
>> exist, you won't get a valid object reference. You simply
>> need to test for that and act

>
> the test is try() the act accordingly is catch(e){}

<snip>

try-catch-finally is designed to handle exceptions; exceptions being
conditions outside of the ability of normal code to detect and control.
Their use in place of normal program flow control logic is
inappropriate, relatively inefficient (lots of overheads in scope chain
modification), incorrect, and should be broadly categorised as a hack.

It doesn't surprise me that you use try-catch for flow control, but as
usual you should not inflict your nonsense on others.

Richard.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Try... except....Try again? MRAB Python 4 07-18-2009 02:53 PM
try -> except -> else -> except? David House Python 2 07-06-2009 05:48 PM
who is simpler? try/except/else or try/except Fabio Z Tessitore Python 5 08-13-2007 12:52 AM
converting a nested try/except statement into try/except/else John Salerno Python 20 08-11-2006 02:48 PM
Try, Try, Try, again... Rick12N4@netscape.net Computer Support 3 01-29-2005 04:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57