473,804 Members | 2,536 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sintax for try and except istruction

is there an istruction like the
try .....
except.....
Jul 23 '05 #1
12 1752
VK
> 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.
Jul 23 '05 #2
On Sat, 09 Oct 2004 10:44:35 GMT, SAN CAZIANO <al**********@t in.it> 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.
Jul 23 '05 #3
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" <sc**********@y ahoo.com> wrote in message news:41******** *************@n ews.freenet.de. ..
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.

Jul 23 '05 #4
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.descript ion;
//do something with element id='oSImage'
// alert(errMsg);
}

--
George Hester
_______________ _______________ ____
"George Hester" <he********@hot mail.com> wrote in message news:32******** *************@t wister.nyroc.rr .com...
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" <sc**********@y ahoo.com> wrote in message news:41******** *************@n ews.freenet.de. ..
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.

Jul 23 '05 #5
On Sat, 09 Oct 2004 13:51:59 GMT, George Hester <he********@hot mail.com>
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.
Jul 23 '05 #6
"Michael Winter" <M.******@bluey onder.co.invali d> wrote in message news:opsflw0iii x13kvk@atlantis ...
On Sat, 09 Oct 2004 13:51:59 GMT, George Hester <he********@hot mail.com>
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
_______________ _______________ ____

Jul 23 '05 #7
On Sat, 09 Oct 2004 14:50:32 GMT, George Hester <he********@hot mail.com>
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.
Jul 23 '05 #8
"Michael Winter" <M.******@bluey onder.co.invali d> wrote in message news:opsfl01bhn x13kvk@atlantis ...
On Sat, 09 Oct 2004 14:50:32 GMT, George Hester <he********@hot mail.com>
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
_______________ _______________ ____
Jul 23 '05 #9
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.
Jul 23 '05 #10

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

Similar topics

39
6081
by: Erlend Fuglum | last post by:
Hi everyone, I'm having some trouble sorting lists. I suspect this might have something to do with locale settings and/or character encoding/unicode. Consider the following example, text containing norwegian special characters æ, ø and å. >>> liste =
3
2506
by: George Young | last post by:
I have a bunch of blanket "except:" clauses like: class DatabaseError(StandardError): pass class OperationalError(StandardError): pass try: stuff except _pg.error, msg: raise DatabaseError, "error '%s' in '%s'" % ( msg, sql )
7
9746
by: Robert Brewer | last post by:
Alex Martelli wrote in another thread: > One sign that somebody has moved from "Python newbie" to "good Python > programmer" is exactly the moment they realize why it's wrong to code: > > try: > x = could_raise_an_exception(23) > process(x) > except Exception, err: > deal_with_exception(err)
0
1548
by: John J. Lee | last post by:
Bare "except:", with no exception specified, is nasty because it can easily hide bugs. There is a case where it seemed useful to me in the past. Now it seems like a bad idea to me (but I think I might still be confused about it, hence this post). It's this: When you're processing a input from a file-like object connected to an external source like a network connection, disk file etc, you have to expect broken, and often malicious,...
2
5400
by: Thorsten Kampe | last post by:
What is the best way to except two errors, when the except handling in both cases is the same? "except:" would just except every error and not just those I want. except Attribute error: do_much_stuff except TypeError: do_the_same_stuff
1
1213
by: simon | last post by:
This works in VB: <td <%# myFunction(DataBinder.Eval(Container.DataItem,"column1"))%>> but in C# doesn't. What is the right sintax to include some text between the html tag in C#? I can't found. Thank you, S
20
3933
by: John Salerno | last post by:
I'm starting out with this: try: if int(text) 0: return True else: self.error_message() return False except ValueError: self.error_message()
35
2021
by: Arnaud Delobelle | last post by:
Hi all, Imagine I have three functions a(x), b(x), c(x) that each return something or raise an exception. Imagine I want to define a function that returns a(x) if possible, otherwise b(x), otherwise c(x), otherwise raise CantDoIt. Here are three ways I can think of doing it: ----------
7
1421
by: InuY4sha | last post by:
Hi all :) sorry.. I know this is a very mean question but I can't get the following couple of lines: static int dev_init(struct net_device *dev); struct net_device my_device = { init: dev_init, }; To me it looks something like defining a function and creating a structure that stores a label referencing that function.. what's the scope behind this ? Also I don't get the comma after "dev_init"... Can somebody explain me this sintax please...
0
9711
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9591
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10594
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10087
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7631
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.