Prototype Sucks
Exception handling can be useful when used properly. Otherwise, it’s horrible and interferes with developing and debugging code because errors you depend on to hone in on the right way of doing things can and often are hidden. It’s tempting to put try{}…catch{} blocks around everything because _if_ an error _does_ occur then error messages and nefarious happenings are hidden from the user, but really, such errors shouldn’t be happening in the first place. I would say a well-placed try{} block is appropriate when _your code itself_ may trigger an exceptional condition that should be cleaned up, but try{} should never be used to hide potential incompatibilities between your code and an API or platform, or your code when it communicates with other systems your other code is running on. Okay, that’s vague, how about an example?
“Prototype”:http://prototype.conio.net/ is supposed to be a good Javascript library. It’s widely used, and so should be relatively bug-free and compatible with most Web browsers. So I downloaded and plugged it into a website I’m working on. Opened up an AJAX call, got the response, and alert()’ed the request.responseText. Works great. I really want to look at the response as XML, via request.responseXML. I’m familiar with using the “DOM interface”:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wceappservices5/html/wce50grfxmldommethods.asp, but… nothing I tried was actually doing anything. Yet no errors were flagged, so I had no hints as to what exactly was going wrong. Apparently execution of the callback method for Prototype’s AJAX response is wrapped in a try{} block with an empty catch, so you wouldn’t know if something didn’t work properly in your callback. After plugging away at this for a couple hours, I temporarily gave up.
I’m tempted to give up on Prototype altogether, despite it’s nice syntactic sugar for certain useful scenarios. Or maybe I can adapt it to better suit my needs. (Just because I’m annoyed at the silent try{}…catch{}es, yes.) I started writing a bunch of AJAX functions (in a file called “datapath.js”, so there’s my competing library) a while back, and debugged it in MSIE, Firefox and Opera — it’s not nearly as “elegant” as aspects of Prototype (yet), but I have the advantage of knowing exactly how it works. And those syntactic sugar methods are nice, but are they really necessary? I don’t mind typing out code for a for-loop every once in a while.