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

Wrap function calls in try/except blocks

Thanks to the fact that Safari and other browsers have decided that
window.onerror is not worth supporting, I'm looking for a clean(er)
way to catch errors than simply tossing try/except blocks in every
single function. My initial thought (which strikes me as poor, hence
this post) is something like

function invoke( func_name ) {
var args=[];
for( var idx=1; idx < arguments.length; idx++ ) {
args.push( arguments[idx] );
}
try {
window[func_name].apply( null, args );
}
catch( ex ) {
if( windowOnErrorSupported ) { // figure this out elsewhere
throw ex; // Allow existing code to handle
}
else {
alert( 'Script error in '+func+': '+ex.message );
}
}
}

The creation of an array every time a function is called is what
really sours me on this idea. Also, I'm wondering about how to
determine whether window.onerror will actually work without detecting
the browser type. Obviously I could intentionally generate a script
error and see if it were properly handled by a dummy window.onerror,
but that also strikes me as a bad idea. Thanks heartily in advance
for any ideas.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Oct 3 '05 #1
7 5203
Christopher Benson-Manica wrote:
Thanks to the fact that Safari and other browsers have
decided that window.onerror is not worth supporting, I'm
looking for a clean(er) way to catch errors than simply
tossing try/except blocks in every single function. ...

<snip>

Why don't you just learn to write defensive code that doesn't error-out,
like the rest of us learnt to do before we had the option of using
try-catch?

Richard.
Oct 3 '05 #2
(Sorry for posting from Google; my usual provider is having one of its
periodic unexplained outages.)

Richard Cornford wrote:
Why don't you just learn to write defensive code that doesn't error-out,
like the rest of us learnt to do before we had the option of using
try-catch?


Among other reasons:

1) 99.9% of this code is already written. It will be much
easier to catch errors as they occur rather than add tests to
hundreds of functions.
2) Much of the code is not written by me. It isn't my
decision, solely, to make.
3) It really strikes me as overkill to test every conceivable
error condition on the off chance that a bug will occur at
that point. Checking the value of five variables and the
existence of hundreds of form fields (not an exaggeration) is
really not preferable to just catching the occasional error
and reporting it so that it can be fixed.

Oct 4 '05 #3
Christopher Benson-Manica wrote:
Richard Cornford wrote:
Why don't you just learn to write defensive code that
doesn't error-out, like the rest of us learnt to do
before we had the option of using try-catch?
Among other reasons:

1) 99.9% of this code is already written.


Elaborating bad code is not a good idea. You just increase the ongoing
maintenance cost, and when it eventually becomes necessary to sort the
mess out there will be much more that needs sorting out. You cannot dig
your way out of a hole.
It will be much easier to catch errors as
they occur rather than add tests to
hundreds of functions.
But what you are proposing in rendering all function calls indirect, and
significantly restricting the possible employment of the language (all
functions called must be named properties of the window object, with the
usual implications for crowded global namespaces).
2) Much of the code is not written by me. It
isn't my decision, solely, to make.
Having less than competent colleagues is not an excuse for letting them
needlessly burden an ongoing project, and all of the others working on
that project.
3) It really strikes me as overkill to test every
conceivable error condition on the off chance that
a bug will occur at that point.
Errors are not things that happen randomly at any point. Most of the
time you know enough about the environment, the values you are using and
the actions you are taking to be certain of the outcome. Suitably
testing the few factors that remain unknown is not that much of a
burden.
Checking the value of five variables and the
existence of hundreds of form fields (not an exaggeration) is
really not preferable to just catching the occasional error
and reporting it so that it can be fixed.


If you are planning to fix the bugs before the completion of QA then
there is no reason to do anything. Browsers are quite capable of letting
you know when an uncaught exception is thrown (and being a lot more
explicit about where it was thrown than your code is even attempting).

Richard.
Oct 4 '05 #4
Richard Cornford <Ri*****@litotes.demon.co.uk> wrote:
Elaborating bad code is not a good idea. You just increase the ongoing
maintenance cost, and when it eventually becomes necessary to sort the
mess out there will be much more that needs sorting out. You cannot dig
your way out of a hole.
Whatever hole there is has already grown quite large, I'm afraid.
But what you are proposing in rendering all function calls indirect, and
significantly restricting the possible employment of the language (all
functions called must be named properties of the window object, with the
usual implications for crowded global namespaces).
Well, I did admit that it struck me as a lousy idea, which is why I
posted hoping for better suggestions.
If you are planning to fix the bugs before the completion of QA then
there is no reason to do anything. Browsers are quite capable of letting
you know when an uncaught exception is thrown (and being a lot more
explicit about where it was thrown than your code is even attempting).


The whole point of this exercise is that at least one browser we
attempt to support, Safari, by default handles uncaught exceptions by
completely hiding them. It's a ridiculous state of affairs, IMO.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Oct 4 '05 #5
Christopher Benson-Manica wrote:
<snip>
The whole point of this exercise is that at least one
browser we attempt to support, Safari, by default handles
uncaught exceptions by completely hiding them. It's a
ridiculous state of affairs, IMO.


To default to not popping up an error dialog is normal for web browsers.
Javascript error reports are of no value at all to the majority of users
(and might convince some that they were doing something wrong
themselves, when in fact they are compliantly blameless for errors).

You should only need to enable Safari's script error reporting mechanism
on development and QA machines, and there should be no good reason for
not doing so.

Richard.
Oct 5 '05 #6
Richard Cornford <Ri*****@litotes.demon.co.uk> wrote:
To default to not popping up an error dialog is normal for web browsers.
Javascript error reports are of no value at all to the majority of users
(and might convince some that they were doing something wrong
themselves, when in fact they are compliantly blameless for errors).


The contents of the error report are of no use, I agree. But knowing
an error occurred can be important - if nothing else, it will probably
spur a call to tech support, and a bug will be fixed. In browsers
that support window.onerror, one can catch script errors, report them
(we log them in a SQL database, and have caught many bugs we had never
found in our testing), and give the user a friendly "Oops, the
programmers screwed up, please call tech support" message.

Not popping up an error dialog is one thing, but trying to pretend
that nothing is wrong is ridiculous, IMO. A user shouldn't be able to
click on a button that happens to generate a script error and get no
clues that something is amiss. That is bad both for developers and
end users. Among other things, if a user does find an error in
Safari, how are the developers going to be sure they have reproduced
it if the only way to get useful information from the end user is to
have them edit some random text file so they can enable the JavaScript
console?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Oct 6 '05 #7
Christopher Benson-Manica wrote:
Richard Cornford wrote:
To default to not popping up an error dialog is normal
for web browsers. Javascript error reports are of no
value at all to the majority of users (and might convince
some that they were doing something wrong themselves,
when in fact they are compliantly blameless for errors).
The contents of the error report are of no use, I agree.
But knowing an error occurred can be important - if nothing
else, it will probably spur a call to tech support, and a
bug will be fixed.


Have you any idea how common script errors are on the public Internet?
Any user would soon tire of reporting them all, and rapidly see the
futility as they mostly exist in the first place because the author does
not know how to do any better and so cannot fix them.
In browsers that support window.onerror,
one can catch script errors, report them (we log them in a
SQL database, and have caught many bugs we had never
found in our testing),
But you are not looking to improve the quality of your testing?
and give the user a friendly "Oops,
the programmers screwed up,
Well, you said it. But QA seem to have also screwed up.
please call tech support" message.
There is nothing quite like trying to convince the users that you don't
know what you are doing.
Not popping up an error dialog is one thing, but trying to
pretend that nothing is wrong is ridiculous, IMO. A user
shouldn't be able to click on a button that happens to
generate a script error and get no clues that something is
amiss. That is bad both for developers and end users.
Among other things, if a user does find an error in Safari,
how are the developers going to be sure they have reproduced
it if the only way to get useful information from the end
user is to have them edit some random text file so they can
enable the JavaScript console?


LOL. You are a bunch of cowboys aren't you? (and apparently it is the
browser's fault).

Richard.
Oct 6 '05 #8

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

Similar topics

31
by: Bo Peng | last post by:
Dear list, I have many dictionaries with the same set of keys and I would like to write a function to calculate something based on these values. For example, I have a = {'x':1, 'y':2} b =...
3
by: Janross | last post by:
I'm having trouble with a query that's prohibitively slow. On my free-standing office computer it's fine (well, 2-4 seconds), but on the client's network, it takes at least 5 minutes to run. ...
4
by: atv | last post by:
Whatis the proper way to handle errors from function calls? For example, i normally have a main function, with calls to mine or c functions. Should i check for errors in the functions called...
8
by: name | last post by:
Back again for more critique... <grin> ------------------------------------------------ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #define MAX 10000
10
by: Douglas G | last post by:
I've tried various ideas on this problem, but I don't see word wrapping. Can you point out what is wrong? It's a K&R exercise, and I'm still new to programming. Other pointers would be helpful...
6
by: Todd A. Anderson | last post by:
I have a function foo of which I need to get the address. The problem is that when you say "&foo" (or just foo for that matter), you get the address of this function's entry in a jump table and...
2
by: Hari Sekhon | last post by:
I want to wrap a whole script in try ... except. What is the best way of doing this? Consider the following: - try: import <modules> <CODE>
2
by: =?Utf-8?B?Y2hyaXNiZW4=?= | last post by:
Hi, I have some C++ static library which I would like to wrap and use in C# applications. It appears that I have two options. 1. Wrap the static library as dynamic library (dll), use unmanaged...
6
by: cppnow | last post by:
Hello. I have a strange conceptual problem I'm trying to think about. I would like to build a library that allows the user to do the following: 1) User defined types: The user defines their...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.