This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one 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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript 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: 10067
    
163

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: 10067
    
163

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: 10067
    
163

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)
 
jQuery in Action, 2nd edition
 
subject: Exception Handlers for specific errors in Javascript