This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes Exception Handlers for specific errors in Javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Exception Handlers for specific errors in Javascript" Watch "Exception Handlers for specific errors in Javascript" New topic
Author

Exception Handlers for specific errors in Javascript

Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9947
    
161

This question is not specific to AJAX. I was going through the sample chapter on Observer Pattern which Kris had posted in one of the threads. In that chapter its been mentioned that objects(Exception handlers) can be registered to be notified of errors through Javascript's intrinsic onerror event. The exception handler will then be notified of *any* error that occurs and will be passed the error message, the url of document where error occured and the line number. Isnt there a way through which the exception handler can subscribe to only specific errors so that it will be notified only when that specific error occurs. I could still achieve this by subscribing to all errors and then having a conditional block to check whether the error is the one which i am interested in, but this looks a bit clumsy.


[My Blog] [JavaRanch Journal]
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
With JavaScript you can only have ONE catch block and you can not have multiple ones for different exception types. You could build a large switch statement to catch the errors.

Eric
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9947
    
161

Originally posted by Eric Pascarello:
With JavaScript you can only have ONE catch block and you can not have multiple ones for different exception types. You could build a large switch statement to catch the errors.



Strange. Is it because there are no specific exception types defined in Javascript or is there some other reason behind this?
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
That is the way it is designed.

Look at the following code to see what is in the exception


Eric
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9947
    
161

That is the way it is designed.


Thanks Eric.

I gave it a try and it contained all the details of the exception (message, fileName, lineNumber, stack & name)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception Handlers for specific errors in Javascript
 
Similar Threads
handleFault() methods
Catching non-thrown exceptions
To throw or not to throw?!
unreachable code
my notes on JLS for any1 who needs them !!