how to detect if try/catch exception handling is supported in browser

A

adnanx82

Hi,

Does anyone know how a script can detect whether exception handling
using try/catch blocks is supported in the browser, so that it can use
them to perform better error handling, and a syntax error is not
produced in cases where they are not supported?

I know that IE and Netscape 5 and above support exception handling (and
older versions don't), but I don't know about other browsers.

Thanks,

-Adnan.
 
M

McKirahan

Hi,

Does anyone know how a script can detect whether exception handling
using try/catch blocks is supported in the browser, so that it can use
them to perform better error handling, and a syntax error is not
produced in cases where they are not supported?

I know that IE and Netscape 5 and above support exception handling (and
older versions don't), but I don't know about other browsers.

Thanks,

-Adnan.

Apparently try/catch was added in JavaScript 1.4 (or 1.5) per
http://www.houseoffusion.com/cf_lists/messages.cfm/forumid:4/threadid:40156

JavaScript Version Incompatibilities
http://bclary.com/2004/08/27/javascript-version-incompatibilities
has a script to tell you what JS version your browser supports.

Is there an exception/error handling mechanism in core JavaScript?
http://www.faqts.com/knowledge_base/view.phtml/aid/10601
describes what you want (I think).
 
K

Kenneth Tbibetts

If you test the 'typeof' one of the Error classes that is supported in the
version 3 of EcmaScript you can have high confidence that try-catch is also
supported. The return value will be 'function'. A return value of Object
means the null object, and no support. For instance:

if(typeof(window.TypeError)=='function'){
go ahead and include try catch code.
}

Warning- I remember Netscape 4 would have conniptions if I even included the
word 'try' in the source code.
Netscape choked on try being a protected keyword even if it never got
called.

I test the client support for document.implementation and write a script
element in the head, while the page loads.
You can add a filter for try-catch; but so far every client that passes the
document.implementation test is OK for try catch.

remote script loaded in the document.head as <script type="text/javascript"
src=" test.js"></script>

file test.js:
function loadWch(boo){
var DI= document.implementation;
if (DI && DI.hasFeature && DI.hasFeature('html', '1.0')){
var str= (boo)? 'someCodefilepath': 'otherCodefilepath';

str='<script type= "text/javascript" + src= "'+ str+ '" > <\/s';
document.writeln(str+'cript>');
}
}
loadWch((typeof(window.TypeError)=='function'));
 
K

Kenneth Tbibetts

If you test the 'typeof' one of the Error classes that is supported in the
version 3 of EcmaScript you can have high confidence that try-catch is also
supported.

The return value will be 'function'. A return value of 'object '
means the null object, and no support. For instance:

if(typeof(window.TypeError)=='function'){
go ahead and include try catch code.
}

Warning- I remember Netscape 4 would have conniptions if I even included the
word 'try' in the source code.
Netscape choked on try being a protected keyword even if it never got
called.

I test client support for document.implementation before I include scripts
on a page.

You can add a filter for the Error Type; but so far every client that passes
the
document.implementation test is OK for try catch.

remote script loaded in the document.head:
<script type="text/javascript" src=" test.js"></script>

file test.js:
function loadWch(boo){
var DI= document.implementation;
if (DI && DI.hasFeature && DI.hasFeature('html', '1.0')){
var str= (boo)? 'someCodefilepath': 'otherCodefilepath';

str='<script type= "text/javascript" + src= "'+ str+ '" > <\/s';
document.writeln(str+'cript>');
}
}
loadWch((typeof(window.TypeError)=='function'));
 
A

adnanx82

Thanks for the replies.

What do you guys think of this solution (below)? To simulate the test
for a browser in which try is not implemented, change the line with
"eval('try {......." to some invalid syntax like "eval('cry {....."
.....

Thanks,

-Adnan.
---------------------
<html>
<script>
var trysupport = 'no';

function on_error(msg, url, line)
{
if (trysupport == 'testing')
{
alert ('try/catch not supported');
}
else
{
alert ('some other error');
}
}
window.onerror = on_error;

function checkTrySupport()
{
trysupport = 'testing';
eval('try { trysupport = "yes"; } catch (e) {}');
alert('try/catch supported');
}

checkTrySupport();
</script>
<body>
TRY SUPPORT TEST PAGE
</body>
</html>
 
C

Csaba Gabor

T

Thomas 'PointedEars' Lahn

Does anyone know how a script can detect whether exception handling
using try/catch blocks is supported in the browser, so that it can
use them to perform better error handling, and a syntax error is not
produced in cases where they are not supported?

This is one of the rare occasions where eval() really comes in handy.
Browsers with script engines that do not support exception handling
(try...catch) have a global `onerror' event handler (specified as part
of the core language instead of the DOM in JavaScript prior to v1.4)
that can be used.

In global context:

var _global = this;


Anywhere "before" the following code is executed:

function dummyError()
{
_global.onerror = null;
return true;
}

In local context:

_global.onerror = dummyError;

eval(new Array(
'try {',
' // do what you want',
'} catch (e) {',
' // handle e',
'}').join("\n"));

dummyError();


PointedEars
 
T

Thomas 'PointedEars' Lahn

What do you guys think of this solution (below)? To simulate the test
for a browser in which try is not implemented, change the line with
"eval('try {......." to some invalid syntax like "eval('cry {....."

It is a nice example and the right approach, however it is invalid HTML[1]
and, more important, will fail compilation in an ECMAScript < 3 script
engine if any try...catch block is included later to accomodate the
detected support; to be seen with Netscape Navigator 4, for example.

I am afraid that it is FWIS not possible to make that feature downwards
compatible in a way that you can execute code that would compile in a
ECMAScript 3+ compliant script engine only if such an engine is present.

However, it is entirely possible to force the ECMAScript 3+ compliant
code via eval() to compile and handle the error when fails to compile.
See my other posting[2] for that.


PointedEars
___________
[1] <http://validator.w3.org/>
[2] <[email protected]>
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top