473,386 Members | 1,943 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,386 software developers and data experts.

What is wrong with try...catch in my attempt?

After seeing the following documentation:
http://www.croczilla.com/~alex/refer...t.html#1051663
I decided to try using a try/catch block in my own JavaScript test
file. But it didn't work. Here's the complete file:

<html>
<head>
</head>

<body>
<script type="text/javascript">
function doParse() {
var txt = document.forms[0].inNum.value
try {
obj = new Packages.java.math.BigInteger(txt)
document.forms[0].outRes.value = "It's an integer!"
return
} catch (ex) {
document.forms[0].outRes.value = "(BigInteger parse failed)"
}
}
</script>
<form>
Type your input here: <input type="text" name="inNum" size="10">
<input type="button" onclick="doParse()"
value="Try to parse that">
<br />
See the parse result here: <input type="text" name="outRes" size="30">
</form>
</body>

</html>

and here's the first error it reported:

try is a reserved identifier.

try {
....^

(After that error caused doParse not to be defined, of course it then
reported that doParse wasn't defined where I tried to use it later.)
Jul 23 '05 #1
5 1857
Robert Maas, see http://tinyurl.com/uh3t wrote:
<snip>
and here's the first error it reported:

try is a reserved identifier.

try {
...^

<snip>

Try-catch was introduced in ECMAScript 3rd edition, JavaScript 1.4 and
JScript 5, prior to that it was a reserved word and its inclusion in a
script was a syntax error. As a result it is almost never used in
Internet browser scripts because syntax errors cannot be recovered from
and cleanly handled.

Fortunately there is almost no reason to use try-catch in Internet
browser scripts as most error-producing activity can be identified
up-front with proper feature detection.

Richard.
Jul 23 '05 #2

Robert Maas, see http://tinyurl.com/uh3t wrote:
and here's the first error it reported:

try is a reserved identifier.

try {
...^


Are you using an old browser like Netscape 4 or IE 4 (IE 4 where the
JScript engine has not been updated)? Then indeed try/catch is not
implemented and simply gives you a syntax error which is why try/catch
so far is hardly used on the web.

There is no need for try/catch to check for an object you can simply do

if (typeof Packages != 'undefined' &&
typeof Packages.java != 'undefined' &&
typeof Packages.java.math != 'undefined' &&
typeof Packages.java.math.BigInteger != 'undefined')

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #3

"Robert Maas, see http://tinyurl.com/uh3t" <re*****@Yahoo.Com> wrote in
message news:RE***************@Yahoo.Com...
<script type="text/javascript">
function doParse() {
var txt = document.forms[0].inNum.value
try {
obj = new Packages.java.math.BigInteger(txt)
document.forms[0].outRes.value = "It's an integer!"
return
} catch (ex) {
document.forms[0].outRes.value = "(BigInteger parse failed)"
}
}
</script>

Hi Robert,

On first glance, it appears as if you don't have any semi-colons.

Put a semi-colon at the end of each line. Specifically, the try error is
because you are missing a semi-colon at the end of this line.
var txt = document.forms[0].inNum.value

good luck.

--
Regards,
John MacIntyre
http://www.johnmacintyre.ca
Specializing in; Database, Web-Applications, and Windows Software


Jul 23 '05 #4
John wrote:
<snip>
On first glance, it appears as if you don't have any
semi-colons.

Put a semi-colon at the end of each line. Specifically,
the try error is because you are missing a semi-colon at
the end of this line. var txt = document.forms[0].inNum.value


Not true and irrelevant. Lines have nothing to do with semicolons in
javascript. Semicolons terminate a sub-set of statements, and statements
may include line breaks wherever not explicitly excluded by the language
production rules. But javascript interpreters are required (by
specification) to do 'automatic semicolon insertion' so
statement-terminating semicolons required by the language's production
rules, but missing form source text, are usually inserted by the
interpreter, preventing their omission from being error-producing.

Richard.
Jul 23 '05 #5
John wrote:
"Robert Maas, see http://tinyurl.com/uh3t" <re*****@Yahoo.Com> wrote in
message news:RE***************@Yahoo.Com...
<script type="text/javascript">
function doParse() {
var txt = document.forms[0].inNum.value
try {
obj = new Packages.java.math.BigInteger(txt)
document.forms[0].outRes.value = "It's an integer!"
return
} catch (ex) {
document.forms[0].outRes.value = "(BigInteger parse failed)"
}
}
</script>


Hi Robert,

On first glance, it appears as if you don't have any semi-colons.

Put a semi-colon at the end of each line. Specifically, the try error is
because you are missing a semi-colon at the end of this line.
var txt = document.forms[0].inNum.value


No, the try error is because the browser/UA he is using doesn't support
it and throws a syntax error that try is a reserved word.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Jul 23 '05 #6

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

Similar topics

24
by: Steven T. Hatton | last post by:
If I understand correctly, I have no assurance that I can determine the type of a simple class instance thrown as an exception unless I explicitly catch it by name. (non-derived classes having no...
4
by: beta2000 | last post by:
I want to try Refresh() again if the first Refresh() fails. Code: try { try { Refresh(); // ... first attempt... } catch(...) {
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.