Thursday, July 14, 2011

Try catch doesnot catch ActiveXObject errors

I had been thinking about making a post on this for a very long time. Unfortunately for some reason , I kept forgetting about it from time to time. And at last I have even forgot that I faced such an issue in the past until recently.

As per my understanding errors that occur due to automation object creation failures when attempting to create ActiveX Objects can't be handled by try catch block. Instead, one must check it right up front to give the desired error message to the user.

the following is a simple illustration of what i am speaking about,

try{
...
var obj=new ActiveXObject("Blah blah blah");   // suppose error occurs in this statement
...
} catch (activeXError) {
  // the error that occured due to active x object creation failure does not get captured here and hence any
  //code that you exectute here does not get executed. Rather you can find an abrupt error message in the
  //console
}

So in order to handle it, you must check it upfront,

if (window.ActiveXObject) {
  ... blah blah blah....
} else {
  // report error
}

Hope that helps :)

No comments:

Post a Comment