Daniel Fournier wrote:
John Bandhauer wrote:

'instanceof' is not really a shortcut, it is just another way of producing the same state. The one important difference between 'instanceof' and 'QueryInterface' in this context is that 'instanceof' will simply return true or false. But, 'QueryInterface' will actually throw a JavaScript exception if the underlying object does not support the given interface.

John.


Thanks for this fine answer John. So to get the 'QueryInterface' checking ability, I just have to do:

if(!(rsc instanceof Components.interfaces.nsIRDFLiteral) throw 'NS_ERROR_XPC_INTERFACE_NOT_FROM_NSISUPPORTS';

Is this eeror code correct?

I'm not sure why you'd want to do that. If you want to do this check without having an exception thrown then use instanceof. Else, just use QueryInterface. The same amount of work is done either way.


Nevertheless, if you did want to closely emulate the exception thrown by QueryInterface then you'd need to throw an nsIXPCException object rather than a string. It would look something like:

throw new Components.Exception("interface not supported",
               Components.results.NS_NOINTERFACE);

Components.Exception and Components.results are documented in:
http://mozilla.org/scriptable/components_object.html

John.




Reply via email to