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

  • Thread starter Robert Maas, see http://tinyurl.com/uh3t
  • Start date
R

Robert Maas, see http://tinyurl.com/uh3t

After seeing the following documentation:
http://www.croczilla.com/~alex/reference/javascript_ref/stmt.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.)
 
R

Richard Cornford

Robert Maas, see http://tinyurl.com/uh3t wrote:
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.
 
M

Martin Honnen

Robert said:
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')
 
J

John

<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.
 
R

Richard Cornford

John wrote:
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.
 
R

Randy Webb

John said:
<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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top