Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception Handlers for specific errors in Javascript

 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is the way it is designed.

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


Eric
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic