Fabulous Adventures In Coding
Eric Lippert is a principal developer on the C# compiler team. Learn more about Eric.
now and then -- like, say, this morning -- someone sends me this mail:
getting an error in my JScript program. The
error number is -2147024877. No description. Help!
sense of those error numbers requires some delving into the depths of how COM represents
errors -- the HRESULT.
HRESULT is a 32 bit unsigned integer where the high bit indicates whether it is an
error or a success. The remaining bits
in the high word indicate the "facility" of the error -- into what broad category
does this error fall? The low word indicates
the specific error for that facility.
are therefore usually talked about in hex, as the bit structure is a lot easier to
read in hex! Consider 0x80070013, for
example. The high bit is set, so this
is an error. The facility code is 7 and
the error code is 0x0013 = 19 in decimal.
JScript interprets the 32 bit error code as a signed integer
and displays it in decimal. No
problem -- just convert that thing back to hex, right?
not quite. JScript doesn't know that
you want this as an unsigned number, so it converts it to a signed hex number, -0x7ff8ffed. We
need to convert this thing to the value it would have been had JScript interpreted
it as an unsigned number in the first place. A
handy fact to know is that the difference between an unsigned number interpreted as
a signed number and the same number interpreted as an unsigned number is always 0x100000000
if the high bit is set, 0 otherwise.
we go. That prints out 80070013. Or,
even better, we could just write a program that takes the error apart:
(hr < 0 )
(hr & 0x80000000)
facility = (hr & 0x7FFF0000) >> 16;
" + facility);
scode = hr & 0x0000FFFF;
" + scode);
facility codes are as follows
you can see that our example is a Windows operating system error (facility 7), and
looking up error 19 we see that this is ERROR_WRITE_PROTECT -- someone is trying to
write to a write-protected floppy probably.
the errors generated by the script engines -- syntax errors, for example -- are FACILITY_CONTROL,
and the error numbers vary between script engines. VB
also uses FACILITY_CONTROL, but fortunately VBScript assigns the same meanings to
the errors as VB does. But in general,
if you get a FACILITY_CONTROL error you need to know what control generated the error
-- VBScript, JScript, a third party control, what? Because
each control can define their own errors, and there may be collisions.
here are some commonly encountered HRESULTs:
E_UNEXPECTED 0x8000FFFF "Catestrophic
failure" -- something completely unexpected has happened.
E_NOTIMPL 0x80004001 "Not
implemented" -- the developer never got around to writing the method you just called!
pretty obvious what happened here
E_INVALIDARG 0x80070057 --
you passed a bad argument to a method
COM is asking an object for an interface. This
can happen if you try to script an object that doesn't support IDispatch.
E_ABORT 0x80004004 --
whatever you were doing was terminated
E_FAIL 0x80004005 --
something failed and we don't know what.
finally, here are three that you should see only rarely from script, but script hosts
may see them moving around in memory and wonder what is going on:
SCRIPT_E_RECORDED 0x86664004 --
this is how we internally track whether the details of an error have been recorded
in the error object or not. We need a
way to say "yes, there was an error, but do not attempt to record information about
SCRIPT_E_PROPAGATE 0x80020102 --
another internal code that we use to track the case where a recorded error is being
propagated up the call stack to a waiting catch handler.
SCRIPT_E_REPORTED 0x80020101 --
the script engines return this to the host when there has been an unhandled error
that the host has already been informed about via OnScriptError.
a pretty bare-bones look at error codes, but it should at least get you started next
time you have a confusing error number.
Reader Shaka comments on my post about error messages that "catastrophic failure" really does take the
Thx to Jay Hugard too for his code
I have a scriptable COM component (implements IDispatchEx) which has a method that takes a script-function as a parameter (an IDispatch* as a parameter). My COM component calls back into script via that supplied IDispatch parameter.
If the script-function happens to throw an exception, then my call to Invoke will return SCRIPT_E_PROPAGATE. If I return that HRESULT back to the caller of my COM component, then the script engine throws an error.
However, the *ACTUAL ERROR* is nowhere to be found. It's not returned in the Invoke HRESULT (that's SCRIPT_E_PROPAGATE). It's not returned via the EXCEPINFO supplied to Invoke (the structure remains empty). AND, it's not available via GetErrorInfo (the call returns S_FALSE)!
I'd *really* like to get my hands on that error, because it would be useful to cache it and return the same error on subsequent calls (getting to the error often involves an expensive operation that is defined by the script-function passed as a parameter, but I do know how to cache the error). Is there a way for a scripted COM component to get to an exception thrown during a call-back into a supplied script-function???
Hmm. I have not looked at that code for ten years (and I didn't understand it real well then; I didn't write the exception handling logic), so I'm a bit rusty. The EXCEPINFO ought to be filled in with the details. Do you know what exactly is being thrown by the script? Do you know if there are any possible handlers up the stack? The engine should interrogate the IDispatchEx to see if it can determine a call stack that has a catch block. Is there any such call stack?
Thanks for responding, Eric!
Worked this out after tracing into the assembly for the JScript engine. See the following Question & Answer on StackOverflow.
Man, was that a tough mystery to unravel ;-) Seriously underdocumented!