Bug 418025 - IA2 - case by case analysis of exception return values, r=aaronlev, a=beltzner
authorsurkov.alexander@gmail.com
Sat, 29 Mar 2008 20:24:02 -0700
changeset 13730 825c8ac6939bad09dda22ea29039b1e98d314788
parent 13729 6ac54150426d212dd9899494ef9233d45aab863b
child 13731 d34322d08226da7e77e02dc1f4fcc4dda5a71495
push idunknown
push userunknown
push dateunknown
reviewersaaronlev, beltzner
bugs418025
milestone1.9pre
Bug 418025 - IA2 - case by case analysis of exception return values, r=aaronlev, a=beltzner
accessible/src/base/nsAccessNode.cpp
accessible/src/base/nsAccessible.cpp
accessible/src/base/nsAccessibleRelation.cpp
accessible/src/base/nsApplicationAccessible.cpp
accessible/src/base/nsApplicationAccessible.h
accessible/src/html/nsHTMLImageAccessible.cpp
accessible/src/html/nsHyperTextAccessible.cpp
accessible/src/msaa/CAccessibleAction.cpp
accessible/src/msaa/CAccessibleComponent.cpp
accessible/src/msaa/CAccessibleEditableText.cpp
accessible/src/msaa/CAccessibleHyperlink.cpp
accessible/src/msaa/CAccessibleHypertext.cpp
accessible/src/msaa/CAccessibleImage.cpp
accessible/src/msaa/CAccessibleTable.cpp
accessible/src/msaa/CAccessibleText.cpp
accessible/src/msaa/CAccessibleValue.cpp
accessible/src/msaa/nsAccessNodeWrap.cpp
accessible/src/msaa/nsAccessNodeWrap.h
accessible/src/msaa/nsAccessibleRelationWrap.cpp
accessible/src/msaa/nsAccessibleWrap.cpp
accessible/src/msaa/nsApplicationAccessibleWrap.cpp
--- a/accessible/src/base/nsAccessNode.cpp
+++ b/accessible/src/base/nsAccessNode.cpp
@@ -441,17 +441,18 @@ nsAccessNode::GetInnerHTML(nsAString& aI
   NS_ENSURE_TRUE(domNSElement, NS_ERROR_NULL_POINTER);
 
   return domNSElement->GetInnerHTML(aInnerHTML);
 }
 
 NS_IMETHODIMP
 nsAccessNode::ScrollTo(PRUint32 aScrollType)
 {
-  NS_ENSURE_TRUE(mDOMNode, NS_ERROR_FAILURE);
+  if (IsDefunct())
+    return NS_ERROR_FAILURE;
 
   nsCOMPtr<nsIPresShell> shell(GetPresShell());
   NS_ENSURE_TRUE(shell, NS_ERROR_FAILURE);
 
   nsIFrame *frame = GetFrame();
   NS_ENSURE_TRUE(frame, NS_ERROR_FAILURE);
 
   nsCOMPtr<nsIContent> content = frame->GetContent();
--- a/accessible/src/base/nsAccessible.cpp
+++ b/accessible/src/base/nsAccessible.cpp
@@ -2918,20 +2918,25 @@ nsAccessible::GetRelation(PRUint32 aInde
 
   nsCOMPtr<nsIArray> relations;
   nsresult rv = GetRelations(getter_AddRefs(relations));
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsCOMPtr<nsIAccessibleRelation> relation;
   rv = relations->QueryElementAt(aIndex, NS_GET_IID(nsIAccessibleRelation),
                                  getter_AddRefs(relation));
+
+  // nsIArray::QueryElementAt() returns NS_ERROR_ILLEGAL_VALUE on invalid index.
+  if (rv == NS_ERROR_ILLEGAL_VALUE)
+    return NS_ERROR_INVALID_ARG;
+
   NS_ENSURE_SUCCESS(rv, rv);
 
   NS_IF_ADDREF(*aRelation = relation);
-  return rv;
+  return NS_OK;
 }
 
 NS_IMETHODIMP
 nsAccessible::GetRelations(nsIArray **aRelations)
 {
   NS_ENSURE_ARG_POINTER(aRelations);
 
   nsCOMPtr<nsIMutableArray> relations = do_CreateInstance(NS_ARRAY_CONTRACTID);
@@ -3229,20 +3234,22 @@ NS_IMETHODIMP nsAccessible::GetURI(PRInt
 {
   *aURI = nsnull;
   return NS_ERROR_FAILURE;
 }
 
 NS_IMETHODIMP nsAccessible::GetObject(PRInt32 aIndex,
                                       nsIAccessible **aAccessible)
 {
-  if (aIndex != 0) {
-    *aAccessible = nsnull;
-    return NS_ERROR_FAILURE;
-  }
+  NS_ENSURE_ARG_POINTER(aAccessible);
+  *aAccessible = nsnull;
+
+  if (aIndex != 0)
+    return NS_ERROR_INVALID_ARG;
+
   *aAccessible = this;
   NS_ADDREF_THIS();
   return NS_OK;
 }
 
 // nsIAccessibleHyperLink::IsValid()
 NS_IMETHODIMP nsAccessible::IsValid(PRBool *aIsValid)
 {
--- a/accessible/src/base/nsAccessibleRelation.cpp
+++ b/accessible/src/base/nsAccessibleRelation.cpp
@@ -69,16 +69,19 @@ nsAccessibleRelation::GetTargetsCount(PR
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsAccessibleRelation::GetTarget(PRUint32 aIndex, nsIAccessible **aTarget)
 {
   NS_ENSURE_ARG_POINTER(aTarget);
 
+  if (aIndex != 0)
+    return NS_ERROR_INVALID_ARG;
+
   NS_IF_ADDREF(*aTarget = mTarget);
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsAccessibleRelation::GetTargets(nsIArray **aRelations)
 {
   NS_ENSURE_ARG_POINTER(aRelations);
--- a/accessible/src/base/nsApplicationAccessible.cpp
+++ b/accessible/src/base/nsApplicationAccessible.cpp
@@ -175,16 +175,25 @@ NS_IMETHODIMP
 nsApplicationAccessible::GetPreviousSibling(nsIAccessible **aPreviousSibling)
 {
   NS_ENSURE_ARG_POINTER(aPreviousSibling);
 
   *aPreviousSibling = nsnull;
   return NS_OK;
 }
 
+NS_IMETHODIMP
+nsApplicationAccessible::GetIndexInParent(PRInt32 *aIndexInParent)
+{
+  NS_ENSURE_ARG_POINTER(aIndexInParent);
+
+  *aIndexInParent = -1;
+  return NS_OK;
+}
+
 void
 nsApplicationAccessible::CacheChildren()
 {
   if (!mChildren) {
     mAccChildCount = eChildCountUninitialized;
     return;
   }
 
--- a/accessible/src/base/nsApplicationAccessible.h
+++ b/accessible/src/base/nsApplicationAccessible.h
@@ -71,16 +71,17 @@ public:
   NS_IMETHOD GetName(nsAString & aName);
   NS_IMETHOD GetDescription(nsAString & aDescription);
   NS_IMETHOD GetRole(PRUint32 *aRole);
   NS_IMETHOD GetFinalRole(PRUint32 *aFinalRole);
   NS_IMETHOD GetState(PRUint32 *aState, PRUint32 *aExtraState);
   NS_IMETHOD GetParent(nsIAccessible * *aParent);
   NS_IMETHOD GetNextSibling(nsIAccessible * *aNextSibling);
   NS_IMETHOD GetPreviousSibling(nsIAccessible **aPreviousSibling);
+  NS_IMETHOD GetIndexInParent(PRInt32 *aIndexInParent);
   NS_IMETHOD GetChildAt(PRInt32 aChildNum, nsIAccessible **aChild);
 
   // nsApplicationAccessible
   virtual nsresult AddRootAccessible(nsIAccessible *aRootAccWrap);
   virtual nsresult RemoveRootAccessible(nsIAccessible *aRootAccWrap);
 
 protected:
   // nsAccessible
--- a/accessible/src/html/nsHTMLImageAccessible.cpp
+++ b/accessible/src/html/nsHTMLImageAccessible.cpp
@@ -269,17 +269,20 @@ nsHTMLImageAccessible::GetObject(PRInt32
 
   if (!mMapElement)
     return nsLinkableAccessible::GetObject(aIndex, aAccessible);
 
   nsCOMPtr<nsIDOMHTMLCollection> mapAreas = GetAreaCollection();
   if (mapAreas) {
     nsCOMPtr<nsIAccessible> accessible;
     accessible = GetAreaAccessible(mapAreas, aIndex);
-    NS_IF_ADDREF(*aAccessible = accessible);
+    if (!accessible)
+      return NS_ERROR_INVALID_ARG;
+
+    NS_ADDREF(*aAccessible = accessible);
   }
 
   return NS_OK;
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 // nsIAccessibleImage
 
--- a/accessible/src/html/nsHyperTextAccessible.cpp
+++ b/accessible/src/html/nsHyperTextAccessible.cpp
@@ -1284,32 +1284,32 @@ NS_IMETHODIMP nsHyperTextAccessible::Get
     if (IsEmbeddedObject(accessible)) {
       ++*aLinks;
     }
   }
   return NS_OK;
 }
 
 
-NS_IMETHODIMP nsHyperTextAccessible::GetLink(PRInt32 aIndex, nsIAccessibleHyperLink **aLink)
+NS_IMETHODIMP
+nsHyperTextAccessible::GetLink(PRInt32 aIndex, nsIAccessibleHyperLink **aLink)
 {
+  NS_ENSURE_ARG_POINTER(aLink);
   *aLink = nsnull;
-  if (!mDOMNode) {
+
+  if (IsDefunct())
     return NS_ERROR_FAILURE;
-  }
 
   nsCOMPtr<nsIAccessible> accessible;
+  while (NextChild(accessible)) {
+    if (IsEmbeddedObject(accessible) && aIndex-- == 0)
+      return CallQueryInterface(accessible, aLink);
+  }
 
-  while (NextChild(accessible)) {
-    if (IsEmbeddedObject(accessible) && aIndex-- == 0) {
-      CallQueryInterface(accessible, aLink);
-      return NS_OK;
-    }
-  }
-  return NS_OK;
+  return NS_ERROR_INVALID_ARG;
 }
 
 NS_IMETHODIMP nsHyperTextAccessible::GetLinkIndex(PRInt32 aCharIndex, PRInt32 *aLinkIndex)
 {
   *aLinkIndex = -1; // API says this magic value means 'not found'
 
   PRInt32 characterCount = 0;
   PRInt32 linkIndex = 0;
--- a/accessible/src/msaa/CAccessibleAction.cpp
+++ b/accessible/src/msaa/CAccessibleAction.cpp
@@ -65,101 +65,102 @@ CAccessibleAction::QueryInterface(REFIID
 }
 
 // IAccessibleAction
 
 STDMETHODIMP
 CAccessibleAction::nActions(long *aNumActions)
 {
 __try {
+  *aNumActions = 0;
+
   nsCOMPtr<nsIAccessible> acc(do_QueryInterface(this));
   if (!acc)
     return E_FAIL;
 
   PRUint8 count = 0;
   nsresult rv = acc->GetNumActions(&count);
-  *aNumActions = count;
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
 
-  if (NS_SUCCEEDED(rv))
-    return NS_OK;
+  *aNumActions = count;
+  return S_OK;
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleAction::doAction(long aActionIndex)
 {
 __try {
   nsCOMPtr<nsIAccessible> acc(do_QueryInterface(this));
   if (!acc)
     return E_FAIL;
 
   PRUint8 index = static_cast<PRUint8>(aActionIndex);
-  if (NS_SUCCEEDED(acc->DoAction(index)))
-    return S_OK;
+  nsresult rv = acc->DoAction(index);
+  return GetHRESULT(rv);
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleAction::get_description(long aActionIndex, BSTR *aDescription)
 {
 __try {
   *aDescription = NULL;
 
   nsCOMPtr<nsIAccessible> acc(do_QueryInterface(this));
   if (!acc)
     return E_FAIL;
 
   nsAutoString description;
   PRUint8 index = static_cast<PRUint8>(aActionIndex);
-  if (NS_FAILED(acc->GetActionDescription(index, description)))
-    return E_FAIL;
+  nsresult rv = acc->GetActionDescription(index, description);
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
 
   if (description.IsVoid())
     return S_FALSE;
 
   *aDescription = ::SysAllocStringLen(description.get(),
                                       description.Length());
-  if (!*aDescription)
-    return E_OUTOFMEMORY;
+  return *aDescription ? S_OK : E_OUTOFMEMORY;
 
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
-  return S_OK;
+  return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleAction::get_keyBinding(long aActionIndex, long aNumMaxBinding,
-                                 BSTR **aKeyBinding,
-                                 long *aNumBinding)
+                                  BSTR **aKeyBinding,
+                                  long *aNumBinding)
 {
 __try {
   *aKeyBinding = NULL;
   *aNumBinding = 0;
 
   nsCOMPtr<nsIAccessible> acc(do_QueryInterface(this));
   if (!acc)
     return E_FAIL;
 
   nsCOMPtr<nsIDOMDOMStringList> keys;
   PRUint8 index = static_cast<PRUint8>(aActionIndex);
   nsresult rv = acc->GetKeyBindings(index, getter_AddRefs(keys));
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   PRUint32 length = 0;
   keys->GetLength(&length);
-
-  PRBool aUseNumMaxBinding = length > static_cast<PRUint32>(aNumMaxBinding);
+  if (length == 0)
+    return S_FALSE;
 
   PRUint32 maxBinding = static_cast<PRUint32>(aNumMaxBinding);
-
   PRUint32 numBinding = length > maxBinding ? maxBinding : length;
   *aNumBinding = numBinding;
 
   *aKeyBinding = static_cast<BSTR*>(nsMemory::Alloc((numBinding) * sizeof(BSTR*)));
   if (!*aKeyBinding)
     return E_OUTOFMEMORY;
 
   PRBool outOfMemory = PR_FALSE;
@@ -179,46 +180,46 @@ CAccessibleAction::get_keyBinding(long a
     for (PRUint32 j = 0; j < i; j++)
       ::SysFreeString(*(aKeyBinding[j]));
 
     nsMemory::Free(*aKeyBinding);
     *aKeyBinding = NULL;
 
     return E_OUTOFMEMORY;
   }
-} __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
+  return S_OK;
 
-  return S_OK;
+} __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
+  return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleAction::get_name(long aActionIndex, BSTR *aName)
 {
 __try {
   *aName = NULL;
 
   nsCOMPtr<nsIAccessible> acc(do_QueryInterface(this));
   if (!acc)
     return E_FAIL;
 
   nsAutoString name;
   PRUint8 index = static_cast<PRUint8>(aActionIndex);
-  if (NS_FAILED(acc->GetActionName(index, name)))
-    return E_FAIL;
+  nsresult rv = acc->GetActionName(index, name);
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
 
   if (name.IsEmpty())
     return S_FALSE;
 
   *aName = ::SysAllocStringLen(name.get(), name.Length());
-  if (!*aName)
-    return E_OUTOFMEMORY;
+  return *aName ? S_OK : E_OUTOFMEMORY;
 
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
-  return S_OK;
+  return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleAction::get_localizedName(long aActionIndex, BSTR *aLocalizedName)
 {
 __try {
   *aLocalizedName = NULL;
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
--- a/accessible/src/msaa/CAccessibleComponent.cpp
+++ b/accessible/src/msaa/CAccessibleComponent.cpp
@@ -88,52 +88,53 @@ CAccessibleComponent::get_locationInPare
   nsCOMPtr<nsIAccessible> acc(do_QueryInterface(this));
   if (!acc)
     return E_FAIL;
 
   // If the object is not on any screen the returned position is (0,0).
   PRUint32 states = 0;
   nsresult rv = acc->GetFinalState(&states, nsnull);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   if (states & nsIAccessibleStates::STATE_INVISIBLE)
     return S_OK;
 
   PRInt32 x = 0, y = 0, width = 0, height = 0;
   rv = acc->GetBounds(&x, &y, &width, &height);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   nsCOMPtr<nsIAccessible> parentAcc;
   rv = acc->GetParent(getter_AddRefs(parentAcc));
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   // The coordinates of the returned position are relative to this object's
   // parent or relative to the screen on which this object is rendered if it
   // has no parent.
   if (!parentAcc) {
     *aX = x;
     *aY = y;
-    return NS_OK;
+    return S_OK;
   }
 
   // The coordinates of the bounding box are given relative to the parent's
   // coordinate system.
   PRInt32 parentx = 0, parenty = 0;
   rv = acc->GetBounds(&parentx, &parenty, &width, &height);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   *aX = x - parentx;
   *aY = y - parenty;
-} __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
+  return S_OK;
 
-  return S_OK;
+} __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
+  return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleComponent::get_foreground(IA2Color *aForeground)
 {
 __try {
   return GetARGBValueFromCSSProperty(NS_LITERAL_STRING("color"), aForeground);
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
@@ -162,73 +163,74 @@ CAccessibleComponent::GetARGBValueFromCS
   nsCOMPtr<nsIAccessNode> acc(do_QueryInterface(this));
   if (!acc)
     return E_FAIL;
 
   nsCOMPtr<nsIDOMCSSPrimitiveValue> cssValue;
   nsresult rv = acc->GetComputedStyleCSSValue(EmptyString(), aPropName,
                                               getter_AddRefs(cssValue));
   if (NS_FAILED(rv) || !cssValue)
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   nsCOMPtr<nsIDOMRGBColor> rgbColor;
   rv = cssValue->GetRGBColorValue(getter_AddRefs(rgbColor));
   if (NS_FAILED(rv) || !rgbColor)
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   nsCOMPtr<nsIDOMNSRGBAColor> rgbaColor(do_QueryInterface(rgbColor));
   if (!rgbaColor)
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   // get alpha
   nsCOMPtr<nsIDOMCSSPrimitiveValue> alphaValue;
   rv = rgbaColor->GetAlpha(getter_AddRefs(alphaValue));
   if (NS_FAILED(rv) || !alphaValue)
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   float alpha = 0.0;
   rv = alphaValue->GetFloatValue(nsIDOMCSSPrimitiveValue::CSS_NUMBER, &alpha);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   // get red
   nsCOMPtr<nsIDOMCSSPrimitiveValue> redValue;
   rv = rgbaColor->GetRed(getter_AddRefs(redValue));
   if (NS_FAILED(rv) || !redValue)
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   float red = 0.0;
   rv = redValue->GetFloatValue(nsIDOMCSSPrimitiveValue::CSS_NUMBER, &red);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   // get green
   nsCOMPtr<nsIDOMCSSPrimitiveValue> greenValue;
   rv = rgbaColor->GetGreen(getter_AddRefs(greenValue));
   if (NS_FAILED(rv) || !greenValue)
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   float green = 0.0;
   rv = greenValue->GetFloatValue(nsIDOMCSSPrimitiveValue::CSS_NUMBER, &green);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   // get blue
   nsCOMPtr<nsIDOMCSSPrimitiveValue> blueValue;
   rv = rgbaColor->GetBlue(getter_AddRefs(blueValue));
   if (NS_FAILED(rv) || !blueValue)
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   float blue = 0.0;
   rv = blueValue->GetFloatValue(nsIDOMCSSPrimitiveValue::CSS_NUMBER, &blue);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   // compose ARGB value
   *aColorValue = (((IA2Color) blue) << IA2BlueShift) |
                  (((IA2Color) green) << IA2GreenShift) |
                  (((IA2Color) red) << IA2RedShift) |
                  (((IA2Color) (alpha * 0xff)) << IA2AlphaShift);
-} __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
+  return S_OK;
 
-  return S_OK;
+} __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
+  return E_FAIL;
 }
 
--- a/accessible/src/msaa/CAccessibleEditableText.cpp
+++ b/accessible/src/msaa/CAccessibleEditableText.cpp
@@ -77,102 +77,99 @@ CAccessibleEditableText::QueryInterface(
 
 STDMETHODIMP
 CAccessibleEditableText::copyText(long aStartOffset, long aEndOffset)
 {
 __try {
   GET_NSIACCESSIBLEEDITABLETEXT
 
   nsresult rv = textAcc->CopyText(aStartOffset, aEndOffset);
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  return GetHRESULT(rv);
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleEditableText::deleteText(long aStartOffset, long aEndOffset)
 {
 __try {
   GET_NSIACCESSIBLEEDITABLETEXT
 
   nsresult rv = textAcc->DeleteText(aStartOffset, aEndOffset);
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  return GetHRESULT(rv);
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleEditableText::insertText(long aOffset, BSTR *aText)
 {
 __try {
   GET_NSIACCESSIBLEEDITABLETEXT
 
   PRUint32 length = ::SysStringLen(*aText);
   nsAutoString text(*aText, length);
 
   nsresult rv = textAcc->InsertText(text, aOffset);
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  return GetHRESULT(rv);
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleEditableText::cutText(long aStartOffset, long aEndOffset)
 {
 __try {
   GET_NSIACCESSIBLEEDITABLETEXT
 
   nsresult rv = textAcc->CutText(aStartOffset, aEndOffset);
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  return GetHRESULT(rv);
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleEditableText::pasteText(long aOffset)
 {
 __try {
   GET_NSIACCESSIBLEEDITABLETEXT
 
   nsresult rv = textAcc->PasteText(aOffset);
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  return GetHRESULT(rv);
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleEditableText::replaceText(long aStartOffset, long aEndOffset,
                                      BSTR *aText)
 {
 __try {
   GET_NSIACCESSIBLEEDITABLETEXT
 
   nsresult rv = textAcc->DeleteText(aStartOffset, aEndOffset);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   PRUint32 length = ::SysStringLen(*aText);
   nsAutoString text(*aText, length);
 
   rv = textAcc->InsertText(text, aStartOffset);
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  return GetHRESULT(rv);
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleEditableText::setAttributes(long aStartOffset, long aEndOffset,
                                        BSTR *aAttributes)
 {
+__try {
+
+} __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
   return E_NOTIMPL;
 }
--- a/accessible/src/msaa/CAccessibleHyperlink.cpp
+++ b/accessible/src/msaa/CAccessibleHyperlink.cpp
@@ -82,129 +82,134 @@ CAccessibleHyperlink::get_anchor(long aI
 __try {
   VariantInit(aAnchor);
 
   nsCOMPtr<nsIAccessibleHyperLink> acc(do_QueryInterface(this));
   if (!acc)
     return E_FAIL;
 
   nsCOMPtr<nsIAccessible> anchor;
-  acc->GetObject(aIndex, getter_AddRefs(anchor));
-  if (!anchor)
-    return E_FAIL;
+  nsresult rv = acc->GetObject(aIndex, getter_AddRefs(anchor));
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
 
   nsCOMPtr<nsIWinAccessNode> winAccessNode(do_QueryInterface(anchor));
   if (!winAccessNode)
     return E_FAIL;
 
   void *instancePtr = NULL;
-  nsresult rv =  winAccessNode->QueryNativeInterface(IID_IUnknown,
-                                                     &instancePtr);
+  rv =  winAccessNode->QueryNativeInterface(IID_IUnknown, &instancePtr);
   if (NS_FAILED(rv))
     return E_FAIL;
 
   IUnknown *unknownPtr = static_cast<IUnknown*>(instancePtr);
   aAnchor->ppunkVal = &unknownPtr;
   aAnchor->vt = VT_UNKNOWN;
-} __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
+  return S_OK;
 
-  return S_OK;
+} __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
+  return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleHyperlink::get_anchorTarget(long aIndex, VARIANT *aAnchorTarget)
 {
 __try {
   VariantInit(aAnchorTarget);
 
   nsCOMPtr<nsIAccessibleHyperLink> acc(do_QueryInterface(this));
   if (!acc)
     return E_FAIL;
 
   nsCOMPtr<nsIURI> uri;
   nsresult rv = acc->GetURI(aIndex, getter_AddRefs(uri));
   if (NS_FAILED(rv) || !uri)
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   nsCAutoString prePath;
   rv = uri->GetPrePath(prePath);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   nsCAutoString path;
   rv = uri->GetPath(path);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   nsAutoString stringURI;
   AppendUTF8toUTF16(prePath, stringURI);
   AppendUTF8toUTF16(path, stringURI);
 
   aAnchorTarget->vt = VT_BSTR;
   aAnchorTarget->bstrVal = ::SysAllocStringLen(stringURI.get(),
                                                stringURI.Length());
-  if (!aAnchorTarget->bstrVal)
-    return E_OUTOFMEMORY;
+  return aAnchorTarget->bstrVal ? S_OK : E_OUTOFMEMORY;
 
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
-  return S_OK;
+  return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleHyperlink::get_startIndex(long *aIndex)
 {
 __try {
   *aIndex = 0;
 
   nsCOMPtr<nsIAccessibleHyperLink> acc(do_QueryInterface(this));
   if (!acc)
     return E_FAIL;
 
   PRInt32 index = 0;
   nsresult rv = acc->GetStartIndex(&index);
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
+
   *aIndex = index;
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  return S_OK;
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleHyperlink::get_endIndex(long *aIndex)
 {
 __try {
   *aIndex = 0;
 
   nsCOMPtr<nsIAccessibleHyperLink> acc(do_QueryInterface(this));
   if (!acc)
     return E_FAIL;
 
   PRInt32 index = 0;
   nsresult rv = acc->GetEndIndex(&index);
-  *aIndex = index;
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
 
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  *aIndex = index;
+  return S_OK;
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleHyperlink::get_valid(boolean *aValid)
 {
 __try {
+  *aValid = false;
+
   nsCOMPtr<nsIAccessibleHyperLink> acc(do_QueryInterface(this));
   if (!acc)
     return E_FAIL;
 
   PRBool isValid = PR_FALSE;
   nsresult rv = acc->IsValid(&isValid);
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
+
   *aValid = isValid;
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  return S_OK;
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
--- a/accessible/src/msaa/CAccessibleHypertext.cpp
+++ b/accessible/src/msaa/CAccessibleHypertext.cpp
@@ -76,67 +76,73 @@ CAccessibleHypertext::get_nHyperlinks(lo
   *aHyperlinkCount = 0;
 
   nsCOMPtr<nsIAccessibleHyperText> hyperAcc(do_QueryInterface(this));
   if (!hyperAcc)
     return E_FAIL;
 
   PRInt32 count = 0;
   nsresult rv = hyperAcc->GetLinks(&count);
-  *aHyperlinkCount = count;
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
 
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  *aHyperlinkCount = count;
+  return S_OK;
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleHypertext::get_hyperlink(long aIndex,
                                     IAccessibleHyperlink **aHyperlink)
 {
 __try {
   *aHyperlink = NULL;
 
   nsCOMPtr<nsIAccessibleHyperText> hyperAcc(do_QueryInterface(this));
   if (!hyperAcc)
     return E_FAIL;
 
   nsCOMPtr<nsIAccessibleHyperLink> hyperLink;
-  hyperAcc->GetLink(aIndex, getter_AddRefs(hyperLink));
-  if (!hyperLink)
-    return E_FAIL;
+  nsresult rv = hyperAcc->GetLink(aIndex, getter_AddRefs(hyperLink));
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
 
   nsCOMPtr<nsIWinAccessNode> winAccessNode(do_QueryInterface(hyperLink));
   if (!winAccessNode)
     return E_FAIL;
 
   void *instancePtr = NULL;
-  nsresult rv =  winAccessNode->QueryNativeInterface(IID_IAccessibleHyperlink,
-                                                     &instancePtr);
+  rv =  winAccessNode->QueryNativeInterface(IID_IAccessibleHyperlink,
+                                            &instancePtr);
+  if (NS_FAILED(rv))
+    return E_FAIL;
+
   *aHyperlink = static_cast<IAccessibleHyperlink*>(instancePtr);
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  return S_OK;
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleHypertext::get_hyperlinkIndex(long aCharIndex, long *aHyperlinkIndex)
 {
 __try {
   *aHyperlinkIndex = 0;
 
   nsCOMPtr<nsIAccessibleHyperText> hyperAcc(do_QueryInterface(this));
   if (!hyperAcc)
     return E_FAIL;
 
   PRInt32 index = 0;
   nsresult rv = hyperAcc->GetLinkIndex(aCharIndex, &index);
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
+
   *aHyperlinkIndex = index;
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  return S_OK;
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
--- a/accessible/src/msaa/CAccessibleImage.cpp
+++ b/accessible/src/msaa/CAccessibleImage.cpp
@@ -80,28 +80,26 @@ CAccessibleImage::get_description(BSTR *
 
   nsCOMPtr<nsIAccessible> acc(do_QueryInterface(this));
   if (!acc)
     return E_FAIL;
 
   nsAutoString description;
   nsresult rv = acc->GetName(description);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   if (description.IsEmpty())
     return S_FALSE;
 
   *aDescription = ::SysAllocStringLen(description.get(), description.Length());
-  if (!*aDescription)
-    return E_OUTOFMEMORY;
+  return *aDescription ? S_OK : E_OUTOFMEMORY;
 
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
-  return S_OK;
+  return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleImage::get_imagePosition(enum IA2CoordinateType aCoordType,
                                     long *aX,
                                     long *aY)
 {
 __try {
@@ -114,40 +112,43 @@ CAccessibleImage::get_imagePosition(enum
 
   nsCOMPtr<nsIAccessibleImage> imageAcc(do_QueryInterface(this));
   if (!imageAcc)
     return E_FAIL;
 
   PRInt32 x = 0, y = 0;
   nsresult rv = imageAcc->GetImagePosition(geckoCoordType, &x, &y);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   *aX = x;
   *aY = y;
+  return S_OK;
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
 
-  return S_OK;
+  return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleImage::get_imageSize(long *aHeight, long *aWidth)
 {
 __try {
   *aHeight = 0;
   *aWidth = 0;
 
   nsCOMPtr<nsIAccessibleImage> imageAcc(do_QueryInterface(this));
   if (!imageAcc)
     return E_FAIL;
 
   PRInt32 x = 0, y = 0, width = 0, height = 0;
   nsresult rv = imageAcc->GetImageSize(&width, &height);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   *aHeight = width;
   *aWidth = height;
-} __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
+  return S_OK;
 
-  return S_OK;
+} __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
+  return E_FAIL;
 }
 
--- a/accessible/src/msaa/CAccessibleTable.cpp
+++ b/accessible/src/msaa/CAccessibleTable.cpp
@@ -80,84 +80,92 @@ CAccessibleTable::get_accessibleAt(long 
   *aAccessible = NULL;
 
   nsCOMPtr<nsIAccessibleTable> tableAcc(do_QueryInterface(this));
   NS_ASSERTION(tableAcc, CANT_QUERY_ASSERTION_MSG);
   if (!tableAcc)
     return E_FAIL;
 
   nsCOMPtr<nsIAccessible> cell;
-  tableAcc->CellRefAt(aRow, aColumn, getter_AddRefs(cell));
+  nsresult rv = tableAcc->CellRefAt(aRow, aColumn, getter_AddRefs(cell));
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
 
   nsCOMPtr<nsIWinAccessNode> winAccessNode(do_QueryInterface(cell));
   if (!winAccessNode)
     return E_FAIL;
 
   void *instancePtr = NULL;
-  nsresult rv = winAccessNode->QueryNativeInterface(IID_IAccessible2,
-                                                    &instancePtr);
+  rv = winAccessNode->QueryNativeInterface(IID_IAccessible2, &instancePtr);
   if (NS_FAILED(rv))
     return E_FAIL;
 
   *aAccessible = static_cast<IUnknown*>(instancePtr);
-} __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
+  return S_OK;
 
-  return S_OK;
+} __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
+  return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleTable::get_caption(IUnknown **aAccessible)
 {
 __try {
   *aAccessible = NULL;
 
   nsCOMPtr<nsIAccessibleTable> tableAcc(do_QueryInterface(this));
   NS_ASSERTION(tableAcc, CANT_QUERY_ASSERTION_MSG);
   if (!tableAcc)
     return E_FAIL;
 
   nsCOMPtr<nsIAccessible> caption;
-  tableAcc->GetCaption(getter_AddRefs(caption));
+  nsresult rv = tableAcc->GetCaption(getter_AddRefs(caption));
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
+
+  if (!caption)
+    return S_FALSE;
 
   nsCOMPtr<nsIWinAccessNode> winAccessNode(do_QueryInterface(caption));
   if (!winAccessNode)
     return E_FAIL;
 
   void *instancePtr = NULL;
-  nsresult rv = winAccessNode->QueryNativeInterface(IID_IAccessible2,
-                                                    &instancePtr);
+  rv = winAccessNode->QueryNativeInterface(IID_IAccessible2, &instancePtr);
   if (NS_FAILED(rv))
     return E_FAIL;
 
   *aAccessible = static_cast<IUnknown*>(instancePtr);
-} __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
+  return S_OK;
 
-  return S_OK;
+} __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
+  return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleTable::get_childIndex(long aRowIndex, long aColumnIndex,
                                  long *aChildIndex)
 {
 __try {
   *aChildIndex = 0;
 
   nsCOMPtr<nsIAccessibleTable> tableAcc(do_QueryInterface(this));
   NS_ASSERTION(tableAcc, CANT_QUERY_ASSERTION_MSG);
   if (!tableAcc)
     return E_FAIL;
 
   PRInt32 childIndex = 0;
   nsresult rv = tableAcc->GetIndexAt(aRowIndex, aColumnIndex, &childIndex);
-  *aChildIndex = childIndex;
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
 
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  *aChildIndex = childIndex;
+  return S_OK;
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleTable::get_columnDescription(long aColumn, BSTR *aDescription)
 {
 __try {
   *aDescription = NULL;
@@ -165,49 +173,49 @@ CAccessibleTable::get_columnDescription(
   nsCOMPtr<nsIAccessibleTable> tableAcc(do_QueryInterface(this));
   NS_ASSERTION(tableAcc, CANT_QUERY_ASSERTION_MSG);
   if (!tableAcc)
     return E_FAIL;
 
   nsAutoString descr;
   nsresult rv = tableAcc->GetColumnDescription (aColumn, descr);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   if (descr.IsEmpty())
     return S_FALSE;
 
   *aDescription = ::SysAllocStringLen(descr.get(), descr.Length());
-  if (!*aDescription)
-    return E_OUTOFMEMORY;
+  return *aDescription ? S_OK : E_OUTOFMEMORY;
 
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-  return S_OK;
+  return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleTable::get_columnExtentAt(long aRow, long aColumn,
                                      long *nColumnsSpanned)
 {
 __try {
   *nColumnsSpanned = 0;
 
   nsCOMPtr<nsIAccessibleTable> tableAcc(do_QueryInterface(this));
   NS_ASSERTION(tableAcc, CANT_QUERY_ASSERTION_MSG);
   if (!tableAcc)
     return E_FAIL;
 
   PRInt32 columnsSpanned = 0;
   nsresult rv = tableAcc->GetColumnExtentAt(aRow, aColumn, &columnsSpanned);
-  *nColumnsSpanned = columnsSpanned;
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
 
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  *nColumnsSpanned = columnsSpanned;
+  return S_OK;
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleTable::get_columnHeader(IAccessibleTable **aAccessibleTable,
                                    long *aStartingRowIndex)
 {
 __try {
@@ -219,161 +227,172 @@ CAccessibleTable::get_columnHeader(IAcce
   nsCOMPtr<nsIAccessibleTable> tableAcc(do_QueryInterface(this));
   NS_ASSERTION(tableAcc, CANT_QUERY_ASSERTION_MSG);
   if (!tableAcc)
     return E_FAIL;
 
   nsCOMPtr<nsIAccessibleTable> header;
   nsresult rv = tableAcc->GetColumnHeader(getter_AddRefs(header));
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
+
+  if (!header)
+    return S_FALSE;
 
   nsCOMPtr<nsIWinAccessNode> winAccessNode(do_QueryInterface(header));
   if (!winAccessNode)
     return E_FAIL;
 
   void *instancePtr = NULL;
   rv = winAccessNode->QueryNativeInterface(IID_IAccessibleTable, &instancePtr);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   *aAccessibleTable = static_cast<IAccessibleTable*>(instancePtr);
-} __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
+  return S_OK;
 
-  return S_OK;
+} __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
+  return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleTable::get_columnIndex(long aChildIndex, long *aColumnIndex)
 {
 __try {
   *aColumnIndex = 0;
 
   nsCOMPtr<nsIAccessibleTable> tableAcc(do_QueryInterface(this));
   NS_ASSERTION(tableAcc, CANT_QUERY_ASSERTION_MSG);
   if (!tableAcc)
     return E_FAIL;
 
   PRInt32 columnIndex = 0;
   nsresult rv = tableAcc->GetColumnAtIndex(aChildIndex, &columnIndex);
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
+
   *aColumnIndex = columnIndex;
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  return S_OK;
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleTable::get_nColumns(long *aColumnCount)
 {
 __try {
   *aColumnCount = 0;
 
   nsCOMPtr<nsIAccessibleTable> tableAcc(do_QueryInterface(this));
   NS_ASSERTION(tableAcc, CANT_QUERY_ASSERTION_MSG);
   if (!tableAcc)
     return E_FAIL;
 
   PRInt32 columnCount = 0;
   nsresult rv = tableAcc->GetColumns(&columnCount);
-  *aColumnCount = columnCount;
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
 
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  *aColumnCount = columnCount;
+  return S_OK;
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleTable::get_nRows(long *aRowCount)
 {
 __try {
   *aRowCount = 0;
 
   nsCOMPtr<nsIAccessibleTable> tableAcc(do_QueryInterface(this));
   NS_ASSERTION(tableAcc, CANT_QUERY_ASSERTION_MSG);
   if (!tableAcc)
     return E_FAIL;
 
   PRInt32 rowCount = 0;
   nsresult rv = tableAcc->GetRows(&rowCount);
-  *aRowCount = rowCount;
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
 
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  *aRowCount = rowCount;
+  return S_OK;
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleTable::get_nSelectedChildren(long *aChildCount)
 {
 __try {
   *aChildCount = 0;
 
   nsCOMPtr<nsIAccessibleTable> tableAcc(do_QueryInterface(this));
   NS_ASSERTION(tableAcc, CANT_QUERY_ASSERTION_MSG);
   if (!tableAcc)
     return E_FAIL;
 
   PRUint32 count = 0;
   nsresult rv = tableAcc->GetSelectedCellsCount(&count);
-  *aChildCount = count;
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
 
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  *aChildCount = count;
+  return S_OK;
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleTable::get_nSelectedColumns(long *aColumnCount)
 {
 __try {
   *aColumnCount = 0;
 
   nsCOMPtr<nsIAccessibleTable> tableAcc(do_QueryInterface(this));
   NS_ASSERTION(tableAcc, CANT_QUERY_ASSERTION_MSG);
   if (!tableAcc)
     return E_FAIL;
 
   PRUint32 count = 0;
   nsresult rv = tableAcc->GetSelectedColumnsCount(&count);
-  *aColumnCount = count;
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
 
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  *aColumnCount = count;
+  return S_OK;
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleTable::get_nSelectedRows(long *aRowCount)
 {
 __try {
   *aRowCount = 0;
 
   nsCOMPtr<nsIAccessibleTable> tableAcc(do_QueryInterface(this));
   NS_ASSERTION(tableAcc, CANT_QUERY_ASSERTION_MSG);
   if (!tableAcc)
     return E_FAIL;
 
   PRUint32 count = 0;
   nsresult rv = tableAcc->GetSelectedRowsCount(&count);
-  *aRowCount = count;
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
 
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  *aRowCount = count;
+  return S_OK;
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleTable::get_rowDescription(long aRow, BSTR *aDescription)
 {
 __try {
   *aDescription = NULL;
@@ -381,48 +400,48 @@ CAccessibleTable::get_rowDescription(lon
   nsCOMPtr<nsIAccessibleTable> tableAcc(do_QueryInterface(this));
   NS_ASSERTION(tableAcc, CANT_QUERY_ASSERTION_MSG);
   if (!tableAcc)
     return E_FAIL;
 
   nsAutoString descr;
   nsresult rv = tableAcc->GetRowDescription (aRow, descr);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   if (descr.IsEmpty())
     return S_FALSE;
 
   *aDescription = ::SysAllocStringLen(descr.get(), descr.Length());
-  if (!*aDescription)
-    return E_OUTOFMEMORY;
+  return *aDescription ? S_OK : E_OUTOFMEMORY;
 
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-  return S_OK;
+  return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleTable::get_rowExtentAt(long aRow, long aColumn, long *aNRowsSpanned)
 {
 __try {
   *aNRowsSpanned = 0;
 
   nsCOMPtr<nsIAccessibleTable> tableAcc(do_QueryInterface(this));
   NS_ASSERTION(tableAcc, CANT_QUERY_ASSERTION_MSG);
   if (!tableAcc)
     return E_FAIL;
 
   PRInt32 rowsSpanned = 0;
   nsresult rv = tableAcc->GetRowExtentAt(aRow, aColumn, &rowsSpanned);
-  *aNRowsSpanned = rowsSpanned;
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
 
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  *aNRowsSpanned = rowsSpanned;
+  return S_OK;
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleTable::get_rowHeader(IAccessibleTable **aAccessibleTable,
                                 long *aStartingColumnIndex)
 {
 __try {
@@ -434,53 +453,58 @@ CAccessibleTable::get_rowHeader(IAccessi
   nsCOMPtr<nsIAccessibleTable> tableAcc(do_QueryInterface(this));
   NS_ASSERTION(tableAcc, CANT_QUERY_ASSERTION_MSG);
   if (!tableAcc)
     return E_FAIL;
 
   nsCOMPtr<nsIAccessibleTable> header;
   nsresult rv = tableAcc->GetRowHeader(getter_AddRefs(header));
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
+
+  if (!header)
+    return S_FALSE;
 
   nsCOMPtr<nsIWinAccessNode> winAccessNode(do_QueryInterface(header));
   if (!winAccessNode)
     return E_FAIL;
 
   void *instancePtr = NULL;
   rv = winAccessNode->QueryNativeInterface(IID_IAccessibleTable,
                                            &instancePtr);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   *aAccessibleTable = static_cast<IAccessibleTable*>(instancePtr);
-} __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
+  return S_OK;
 
-  return S_OK;
+} __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
+  return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleTable::get_rowIndex(long aChildIndex, long *aRowIndex)
 {
 __try {
   *aRowIndex = 0;
 
   nsCOMPtr<nsIAccessibleTable> tableAcc(do_QueryInterface(this));
   NS_ASSERTION(tableAcc, CANT_QUERY_ASSERTION_MSG);
   if (!tableAcc)
     return E_FAIL;
 
   PRInt32 rowIndex = 0;
   nsresult rv = tableAcc->GetRowAtIndex(aChildIndex, &rowIndex);
-  *aRowIndex = rowIndex;
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
 
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  *aRowIndex = rowIndex;
+  return S_OK;
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleTable::get_selectedChildren(long aMaxChildren, long **aChildren,
                                        long *aNChildren)
 {
 __try {
@@ -506,150 +530,158 @@ CAccessibleTable::get_selectedRows(long 
   return GetSelectedItems(aMaxRows, aRows, aNRows, ITEMSTYPE_ROWS);
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleTable::get_summary(IUnknown **aAccessible)
 {
+__try {
   *aAccessible = NULL;
 
   // Neither html:table nor xul:tree nor ARIA grid/tree have an ability to
   // link an accessible object to specify a summary. There is closes method
   // in nsIAccessibleTable::summary to get a summary as a string which is not
   // mapped directly to IAccessible2.
-  return S_OK;
+
+} __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
+  return S_FALSE;
 }
 
 STDMETHODIMP
 CAccessibleTable::get_isColumnSelected(long aColumn, boolean *aIsSelected)
 {
 __try {
+  *aIsSelected = false;
+
   nsCOMPtr<nsIAccessibleTable> tableAcc(do_QueryInterface(this));
   NS_ASSERTION(tableAcc, CANT_QUERY_ASSERTION_MSG);
   if (!tableAcc)
     return E_FAIL;
 
   PRBool isSelected = PR_FALSE;
   nsresult rv = tableAcc->IsColumnSelected(aColumn, &isSelected);
-  *aIsSelected = isSelected;
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
 
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  *aIsSelected = isSelected;
+  return S_OK;
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleTable::get_isRowSelected(long aRow, boolean *aIsSelected)
 {
 __try {
+  *aIsSelected = false;
+
   nsCOMPtr<nsIAccessibleTable> tableAcc(do_QueryInterface(this));
   NS_ASSERTION(tableAcc, CANT_QUERY_ASSERTION_MSG);
   if (!tableAcc)
     return E_FAIL;
 
   PRBool isSelected = PR_FALSE;
   nsresult rv = tableAcc->IsRowSelected(aRow, &isSelected);
-  *aIsSelected = isSelected;
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
 
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  *aIsSelected = isSelected;
+  return S_OK;
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleTable::get_isSelected(long aRow, long aColumn, boolean *aIsSelected)
 {
 __try {
+  *aIsSelected = false;
+
   nsCOMPtr<nsIAccessibleTable> tableAcc(do_QueryInterface(this));
   NS_ASSERTION(tableAcc, CANT_QUERY_ASSERTION_MSG);
   if (!tableAcc)
     return E_FAIL;
 
   PRBool isSelected = PR_FALSE;
   nsresult rv = tableAcc->IsCellSelected(aRow, aColumn, &isSelected);
-  *aIsSelected = isSelected;
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
 
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  *aIsSelected = isSelected;
+  return S_OK;
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleTable::selectRow(long aRow)
 {
 __try {
   nsCOMPtr<nsIAccessibleTable> tableAcc(do_QueryInterface(this));
   NS_ASSERTION(tableAcc, CANT_QUERY_ASSERTION_MSG);
   if (!tableAcc)
     return E_FAIL;
 
   nsresult rv = tableAcc->SelectRow(aRow);
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  return GetHRESULT(rv);
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleTable::selectColumn(long aColumn)
 {
 __try {
   nsCOMPtr<nsIAccessibleTable> tableAcc(do_QueryInterface(this));
   NS_ASSERTION(tableAcc, CANT_QUERY_ASSERTION_MSG);
   if (!tableAcc)
     return E_FAIL;
 
   nsresult rv = tableAcc->SelectColumn(aColumn);
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  return GetHRESULT(rv);
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleTable::unselectRow(long aRow)
 {
 __try {
   nsCOMPtr<nsIAccessibleTable> tableAcc(do_QueryInterface(this));
   NS_ASSERTION(tableAcc, CANT_QUERY_ASSERTION_MSG);
   if (!tableAcc)
     return E_FAIL;
 
   nsresult rv = tableAcc->UnselectRow(aRow);
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  return GetHRESULT(rv);
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleTable::unselectColumn(long aColumn)
 {
 __try {
   nsCOMPtr<nsIAccessibleTable> tableAcc(do_QueryInterface(this));
   NS_ASSERTION(tableAcc, CANT_QUERY_ASSERTION_MSG);
   if (!tableAcc)
     return E_FAIL;
 
   nsresult rv = tableAcc->UnselectColumn(aColumn);
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  return GetHRESULT(rv);
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleTable::get_rowColumnExtentsAtIndex(long aIndex, long *aRow,
                                               long *aColumn,
                                               long *aRowExtents,
                                               long *aColumnExtents,
@@ -665,52 +697,55 @@ CAccessibleTable::get_rowColumnExtentsAt
   nsCOMPtr<nsIAccessibleTable> tableAcc(do_QueryInterface(this));
   NS_ASSERTION(tableAcc, CANT_QUERY_ASSERTION_MSG);
   if (!tableAcc)
     return E_FAIL;
 
   PRInt32 row = -1;
   nsresult rv = tableAcc->GetRowAtIndex(aIndex, &row);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   PRInt32 column = -1;
   rv = tableAcc->GetColumnAtIndex(aIndex, &column);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   PRInt32 rowExtents = 0;
   rv = tableAcc->GetRowExtentAt(row, column, &rowExtents);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   PRInt32 columnExtents = 0;
   rv = tableAcc->GetColumnExtentAt(row, column, &columnExtents);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   PRBool isSelected = PR_FALSE;
   rv = tableAcc->IsCellSelected(row, column, &isSelected);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   *aRow = row;
   *aColumn = column;
   *aRowExtents = rowExtents;
   *aColumnExtents = columnExtents;
   *aIsSelected = isSelected;
-} __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
+  return S_OK;
 
-  return S_OK;
+} __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
+  return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleTable::get_modelChange(IA2TableModelChange *aModelChange)
 {
-  aModelChange = NULL;
+__try {
+
+} __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
   return E_NOTIMPL;
 }
 
 // CAccessibleTable
 
 HRESULT
 CAccessibleTable::GetSelectedItems(long aMaxItems, long **aItems,
                                    long *aItemsCount, eItemsType aType)
@@ -718,17 +753,17 @@ CAccessibleTable::GetSelectedItems(long 
   *aItemsCount = 0;
 
   nsCOMPtr<nsIAccessibleTable> tableAcc(do_QueryInterface(this));
   NS_ASSERTION(tableAcc, CANT_QUERY_ASSERTION_MSG);
   if (!tableAcc)
     return E_FAIL;
 
   PRUint32 size = 0;
-  PRInt32 *items = NULL;
+  PRInt32 *items = nsnull;
 
   nsresult rv = NS_OK;
   switch (aType) {
     case ITEMSTYPE_CELLS:
       rv = tableAcc->GetSelectedCells(&size, &items);
       break;
     case ITEMSTYPE_COLUMNS:
       rv = tableAcc->GetSelectedColumns(&size, &items);
@@ -736,20 +771,20 @@ CAccessibleTable::GetSelectedItems(long 
     case ITEMSTYPE_ROWS:
       rv = tableAcc->GetSelectedRows(&size, &items);
       break;
     default:
       return E_FAIL;
   }
 
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   if (size == 0 || !items)
-    return S_OK;
+    return S_FALSE;
 
   PRUint32 maxSize = size < static_cast<PRUint32>(aMaxItems) ? size : aMaxItems;
   *aItemsCount = maxSize;
 
   *aItems = static_cast<long*>(nsMemory::Alloc((maxSize) * sizeof(long)));
   if (!*aItems)
     return E_OUTOFMEMORY;
 
--- a/accessible/src/msaa/CAccessibleText.cpp
+++ b/accessible/src/msaa/CAccessibleText.cpp
@@ -83,28 +83,29 @@ CAccessibleText::QueryInterface(REFIID i
 
 STDMETHODIMP
 CAccessibleText::addSelection(long aStartOffset, long aEndOffset)
 {
 __try {
   GET_NSIACCESSIBLETEXT
 
   nsresult rv = textAcc->AddSelection(aStartOffset, aEndOffset);
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  return GetHRESULT(rv);
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleText::get_attributes(long aOffset, long *aStartOffset,
                                 long *aEndOffset, BSTR *aTextAttributes)
 {
 __try {
+  *aStartOffset = 0;
+  *aEndOffset = 0;
   *aTextAttributes = NULL;
 
   GET_NSIACCESSIBLETEXT
 
   nsCOMPtr<nsIAccessible> accessible;
   PRInt32 startOffset = 0, endOffset = 0;
   textAcc->GetAttributeRange(aOffset, &startOffset, &endOffset,
                              getter_AddRefs(accessible));
@@ -122,156 +123,177 @@ CAccessibleText::get_attributes(long aOf
 
   IAccessible2 *pAccessible2 = static_cast<IAccessible2*>(instancePtr);
   HRESULT hr = pAccessible2->get_attributes(aTextAttributes);
   pAccessible2->Release();
 
   *aStartOffset = startOffset;
   *aEndOffset = endOffset;
   return hr;
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-  return E_FAIL;
+  return E_NOTIMPL;
 }
 
 STDMETHODIMP
 CAccessibleText::get_caretOffset(long *aOffset)
 {
 __try {
+  *aOffset = -1;
+
   GET_NSIACCESSIBLETEXT
 
   PRInt32 offset = 0;
   nsresult rv = textAcc->GetCaretOffset(&offset);
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
+
   *aOffset = offset;
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  return S_OK;
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleText::get_characterExtents(long aOffset,
                                       enum IA2CoordinateType aCoordType,
                                       long *aX, long *aY,
                                       long *aWidth, long *aHeight)
 {
 __try {
+  *aX = 0;
+  *aY = 0;
+  *aWidth = 0;
+  *aHeight = 0;
+
   GET_NSIACCESSIBLETEXT
 
   PRUint32 geckoCoordType = (aCoordType == IA2_COORDTYPE_SCREEN_RELATIVE) ?
     nsIAccessibleCoordinateType::COORDTYPE_SCREEN_RELATIVE :
     nsIAccessibleCoordinateType::COORDTYPE_PARENT_RELATIVE;
 
   PRInt32 x = 0, y =0, width = 0, height = 0;
   nsresult rv = textAcc->GetCharacterExtents (aOffset, &x, &y, &width, &height,
                                               geckoCoordType);
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
+
   *aX = x;
   *aY = y;
   *aWidth = width;
   *aHeight = height;
+  return S_OK;
 
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleText::get_nSelections(long *aNSelections)
 {
 __try {
+  *aNSelections = 0;
+
   GET_NSIACCESSIBLETEXT
 
   PRInt32 selCount = 0;
   nsresult rv = textAcc->GetSelectionCount(&selCount);
-  *aNSelections = selCount;
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
 
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  *aNSelections = selCount;
+  return S_OK;
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleText::get_offsetAtPoint(long aX, long aY,
                                    enum IA2CoordinateType aCoordType,
                                    long *aOffset)
 {
 __try {
+  *aOffset = 0;
+
   GET_NSIACCESSIBLETEXT
 
   PRUint32 geckoCoordType = (aCoordType == IA2_COORDTYPE_SCREEN_RELATIVE) ?
     nsIAccessibleCoordinateType::COORDTYPE_SCREEN_RELATIVE :
     nsIAccessibleCoordinateType::COORDTYPE_PARENT_RELATIVE;
 
   PRInt32 offset = 0;
   nsresult rv = textAcc->GetOffsetAtPoint(aX, aY, geckoCoordType, &offset);
-  *aOffset = offset;
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
 
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  *aOffset = offset;
+  return S_OK;
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleText::get_selection(long aSelectionIndex, long *aStartOffset,
                                long *aEndOffset)
 {
 __try {
+  *aStartOffset = 0;
+  *aEndOffset = 0;
+
   GET_NSIACCESSIBLETEXT
 
   PRInt32 startOffset = 0, endOffset = 0;
   nsresult rv = textAcc->GetSelectionBounds(aSelectionIndex,
                                             &startOffset, &endOffset);
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
+
   *aStartOffset = startOffset;
   *aEndOffset = endOffset;
+  return S_OK;
 
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleText::get_text(long aStartOffset, long aEndOffset, BSTR *aText)
 {
 __try {
   *aText = NULL;
 
   GET_NSIACCESSIBLETEXT
 
   nsAutoString text;
   nsresult rv = textAcc->GetText(aStartOffset, aEndOffset, text);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   if (text.IsEmpty())
     return S_FALSE;
 
   *aText = ::SysAllocStringLen(text.get(), text.Length());
-  if (!*aText)
-    return E_OUTOFMEMORY;
+  return *aText ? S_OK : E_OUTOFMEMORY;
 
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
-  return S_OK;
+  return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleText::get_textBeforeOffset(long aOffset,
                                       enum IA2TextBoundaryType aBoundaryType,
                                       long *aStartOffset, long *aEndOffset,
                                       BSTR *aText)
 {
 __try {
+  *aStartOffset = 0;
+  *aEndOffset = 0;
   *aText = NULL;
 
   GET_NSIACCESSIBLETEXT
 
   nsresult rv = NS_OK;
   nsAutoString text;
   PRInt32 startOffset = 0, endOffset = 0;
 
@@ -283,40 +305,40 @@ CAccessibleText::get_textBeforeOffset(lo
     nsAccessibleTextBoundary boundaryType = GetGeckoTextBoundary(aBoundaryType);
     if (boundaryType == -1)
       return S_FALSE;
     rv = textAcc->GetTextBeforeOffset(aOffset, boundaryType,
                                       &startOffset, &endOffset, text);
   }
 
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   *aStartOffset = startOffset;
   *aEndOffset = endOffset;
 
   if (text.IsEmpty())
     return S_FALSE;
 
   *aText = ::SysAllocStringLen(text.get(), text.Length());
-  if (!*aText)
-    return E_OUTOFMEMORY;
+  return *aText ? S_OK : E_OUTOFMEMORY;
 
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
-  return S_OK;
+  return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleText::get_textAfterOffset(long aOffset,
                                      enum IA2TextBoundaryType aBoundaryType,
                                      long *aStartOffset, long *aEndOffset,
                                      BSTR *aText)
 {
 __try {
+  *aStartOffset = 0;
+  *aEndOffset = 0;
   *aText = NULL;
 
   GET_NSIACCESSIBLETEXT
 
   nsresult rv = NS_OK;
   nsAutoString text;
   PRInt32 startOffset = 0, endOffset = 0;
 
@@ -328,40 +350,42 @@ CAccessibleText::get_textAfterOffset(lon
     nsAccessibleTextBoundary boundaryType = GetGeckoTextBoundary(aBoundaryType);
     if (boundaryType == -1)
       return S_FALSE;
     rv = textAcc->GetTextAfterOffset(aOffset, boundaryType,
                                      &startOffset, &endOffset, text);
   }
 
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   *aStartOffset = startOffset;
   *aEndOffset = endOffset;
 
   if (text.IsEmpty())
     return S_FALSE;
 
   *aText = ::SysAllocStringLen(text.get(), text.Length());
-  if (!*aText)
-    return E_OUTOFMEMORY;
+  return *aText ? S_OK : E_OUTOFMEMORY;
 
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
-  return S_OK;
+  return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleText::get_textAtOffset(long aOffset,
                                   enum IA2TextBoundaryType aBoundaryType,
                                   long *aStartOffset, long *aEndOffset,
                                   BSTR *aText)
 {
 __try {
+  *aStartOffset = 0;
+  *aEndOffset = 0;
+  *aText = NULL;
+
   GET_NSIACCESSIBLETEXT
 
   nsresult rv = NS_OK;
   nsAutoString text;
   PRInt32 startOffset = 0, endOffset = 0;
 
   if (aBoundaryType == IA2_TEXT_BOUNDARY_ALL) {
     startOffset = 0;
@@ -371,106 +395,103 @@ CAccessibleText::get_textAtOffset(long a
     nsAccessibleTextBoundary boundaryType = GetGeckoTextBoundary(aBoundaryType);
     if (boundaryType == -1)
       return S_FALSE;
     rv = textAcc->GetTextAtOffset(aOffset, boundaryType,
                                   &startOffset, &endOffset, text);
   }
 
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   *aStartOffset = startOffset;
   *aEndOffset = endOffset;
 
   if (text.IsEmpty())
     return S_FALSE;
 
   *aText = ::SysAllocStringLen(text.get(), text.Length());
-  if (!*aText)
-    return E_OUTOFMEMORY;
+  return *aText ? S_OK : E_OUTOFMEMORY;
 
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
-  return S_OK;
+  return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleText::removeSelection(long aSelectionIndex)
 {
 __try {
   GET_NSIACCESSIBLETEXT
 
   nsresult rv = textAcc->RemoveSelection(aSelectionIndex);
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  return GetHRESULT(rv);
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleText::setCaretOffset(long aOffset)
 {
 __try {
   GET_NSIACCESSIBLETEXT
 
   nsresult rv = textAcc->SetCaretOffset(aOffset);
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  return GetHRESULT(rv);
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleText::setSelection(long aSelectionIndex, long aStartOffset,
                               long aEndOffset)
 {
 __try {
   GET_NSIACCESSIBLETEXT
 
   nsresult rv = textAcc->SetSelectionBounds(aSelectionIndex,
                                             aStartOffset, aEndOffset);
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  return GetHRESULT(rv);
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleText::get_nCharacters(long *aNCharacters)
 {
 __try {
+  *aNCharacters = 0;
+
   GET_NSIACCESSIBLETEXT
 
   PRInt32 charCount = 0;
   nsresult rv = textAcc->GetCharacterCount(&charCount);
-  *aNCharacters = charCount;
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
 
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  *aNCharacters = charCount;
+  return S_OK;
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleText::scrollSubstringTo(long aStartIndex, long aEndIndex,
                                    enum IA2ScrollType aScrollType)
 {
 __try {
   GET_NSIACCESSIBLETEXT
 
   nsresult rv = textAcc->ScrollSubstringTo(aStartIndex, aEndIndex, aScrollType);
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  return GetHRESULT(rv);
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleText::scrollSubstringToPoint(long aStartIndex, long aEndIndex,
                                         enum IA2CoordinateType aCoordType,
                                         long aX, long aY)
 {
@@ -478,60 +499,64 @@ CAccessibleText::scrollSubstringToPoint(
   GET_NSIACCESSIBLETEXT
 
   PRUint32 geckoCoordType = (aCoordType == IA2_COORDTYPE_SCREEN_RELATIVE) ?
     nsIAccessibleCoordinateType::COORDTYPE_SCREEN_RELATIVE :
     nsIAccessibleCoordinateType::COORDTYPE_PARENT_RELATIVE;
 
   nsresult rv = textAcc->ScrollSubstringToPoint(aStartIndex, aEndIndex,
                                                 geckoCoordType, aX, aY);
-  if (NS_SUCCEEDED(rv))
-    return S_OK;
+  return GetHRESULT(rv);
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleText::get_newText(IA2TextSegment *aNewText)
 {
 __try {
   return GetModifiedText(PR_TRUE, aNewText);
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleText::get_oldText(IA2TextSegment *aOldText)
 {
 __try {
   return GetModifiedText(PR_FALSE, aOldText);
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
   return E_FAIL;
 }
 
 // CAccessibleText
 
 HRESULT
 CAccessibleText::GetModifiedText(PRBool aGetInsertedText,
                                  IA2TextSegment *aText)
 {
   PRUint32 startOffset = 0, endOffset = 0;
   nsAutoString text;
 
   nsresult rv = GetModifiedText(aGetInsertedText, text,
                                 &startOffset, &endOffset);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   aText->start = startOffset;
   aText->end = endOffset;
 
+  if (text.IsEmpty())
+    return S_FALSE;
+
   aText->text = ::SysAllocStringLen(text.get(), text.Length());
-  return aText->text ? NS_OK : E_OUTOFMEMORY;
+  return aText->text ? S_OK : E_OUTOFMEMORY;
 }
 
 nsAccessibleTextBoundary
 CAccessibleText::GetGeckoTextBoundary(enum IA2TextBoundaryType aBoundaryType)
 {
   switch (aBoundaryType) {
     case IA2_TEXT_BOUNDARY_CHAR:
       return nsIAccessibleText::BOUNDARY_CHAR;
--- a/accessible/src/msaa/CAccessibleValue.cpp
+++ b/accessible/src/msaa/CAccessibleValue.cpp
@@ -78,79 +78,82 @@ CAccessibleValue::get_currentValue(VARIA
 
   nsCOMPtr<nsIAccessibleValue> valueAcc(do_QueryInterface(this));
   if (!valueAcc)
     return E_FAIL;
 
   double currentValue = 0;
   nsresult rv = valueAcc->GetCurrentValue(&currentValue);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   aCurrentValue->vt = VT_R8;
   aCurrentValue->dblVal = currentValue;
+  return S_OK;
 
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-  return NS_OK;
+  return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleValue::setCurrentValue(VARIANT aValue)
 {
 __try {
   nsCOMPtr<nsIAccessibleValue> valueAcc(do_QueryInterface(this));
   if (!valueAcc)
     return E_FAIL;
 
   if (aValue.vt != VT_R8)
     return E_INVALIDARG;
 
-  if (NS_SUCCEEDED(valueAcc->SetCurrentValue(aValue.dblVal)))
-    return S_OK;
+  nsresult rv = valueAcc->SetCurrentValue(aValue.dblVal);
+  return GetHRESULT(rv);
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleValue::get_maximumValue(VARIANT *aMaximumValue)
 {
 __try {
   VariantInit(aMaximumValue);
 
   nsCOMPtr<nsIAccessibleValue> valueAcc(do_QueryInterface(this));
   if (!valueAcc)
     return E_FAIL;
 
   double maximumValue = 0;
   nsresult rv = valueAcc->GetMaximumValue(&maximumValue);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   aMaximumValue->vt = VT_R8;
   aMaximumValue->dblVal = maximumValue;
-} __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
+  return S_OK;
 
-  return NS_OK;
+} __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
+  return E_FAIL;
 }
 
 STDMETHODIMP
 CAccessibleValue::get_minimumValue(VARIANT *aMinimumValue)
 {
 __try {
   VariantInit(aMinimumValue);
 
   nsCOMPtr<nsIAccessibleValue> valueAcc(do_QueryInterface(this));
   if (!valueAcc)
     return E_FAIL;
 
   double minimumValue = 0;
   nsresult rv = valueAcc->GetMinimumValue(&minimumValue);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   aMinimumValue->vt = VT_R8;
   aMinimumValue->dblVal = minimumValue;
-} __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
+  return S_OK;
 
-  return NS_OK;
+} __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
+  return E_FAIL;
 }
 
--- a/accessible/src/msaa/nsAccessNodeWrap.cpp
+++ b/accessible/src/msaa/nsAccessNodeWrap.cpp
@@ -628,8 +628,30 @@ int nsAccessNodeWrap::FilterA11yExceptio
     }
 #endif
   }
   else {
     NS_NOTREACHED("We should only be catching crash exceptions");
   }
   return EXCEPTION_CONTINUE_SEARCH;
 }
+
+HRESULT
+GetHRESULT(nsresult aResult)
+{
+  switch (aResult) {
+    case NS_OK:
+      return S_OK;
+
+    case NS_ERROR_INVALID_ARG: case NS_ERROR_INVALID_POINTER:
+      return E_INVALIDARG;
+
+    case NS_ERROR_OUT_OF_MEMORY:
+      return E_OUTOFMEMORY;
+
+    case NS_ERROR_NOT_IMPLEMENTED:
+      return E_NOTIMPL;
+
+    default:
+      return E_FAIL;
+  }
+}
+
--- a/accessible/src/msaa/nsAccessNodeWrap.h
+++ b/accessible/src/msaa/nsAccessNodeWrap.h
@@ -166,10 +166,15 @@ class nsAccessNodeWrap :  public nsAcces
 
     /**
      * It is used in nsHyperTextAccessibleWrap for IA2::newText/oldText
      * implementation.
      */
     static nsIAccessibleTextChangeEvent *gTextEvent;
 };
 
+/**
+ * Converts nsresult to HRESULT.
+ */
+HRESULT GetHRESULT(nsresult aResult);
+
 #endif
 
--- a/accessible/src/msaa/nsAccessibleRelationWrap.cpp
+++ b/accessible/src/msaa/nsAccessibleRelationWrap.cpp
@@ -86,19 +86,18 @@ STDMETHODIMP
 nsAccessibleRelationWrap::get_relationType(BSTR *aRelationType)
 {
 __try {
   *aRelationType = NULL;
 
   PRUint32 type = 0;
   nsresult rv = GetRelationType(&type);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
-  INT res;
   switch (type) {
     case RELATION_CONTROLLED_BY:
       *aRelationType = ::SysAllocString(IA2_RELATION_CONTROLLED_BY);
       break;
     case RELATION_CONTROLLER_FOR:
       *aRelationType = ::SysAllocString(IA2_RELATION_CONTROLLER_FOR);
       break;
     case RELATION_DESCRIBED_BY:
@@ -139,107 +138,120 @@ nsAccessibleRelationWrap::get_relationTy
       break;
     case RELATION_SUBWINDOW_OF:
       *aRelationType = ::SysAllocString(IA2_RELATION_SUBWINDOW_OF);
       break;
     default:
       return E_FAIL;
   }
 
-  if (!res)
-    return E_OUTOFMEMORY;
+  return *aRelationType ? S_OK : E_OUTOFMEMORY;
 
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-  
-  return S_OK;
+  return E_FAIL;
 }
 
 STDMETHODIMP
 nsAccessibleRelationWrap::get_localizedRelationType(BSTR *aLocalizedRelationType)
 {
-  ::SysFreeString(*aLocalizedRelationType);
+__try {
+  *aLocalizedRelationType = NULL;
+
+} __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
   return E_NOTIMPL;
 }
 
 STDMETHODIMP
 nsAccessibleRelationWrap::get_nTargets(long *aNTargets)
 {
 __try {
+  *aNTargets = 0;
+
   PRUint32 count = 0;
   nsresult rv = GetTargetsCount(&count);
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
+
   *aNTargets = count;
-  if (NS_FAILED(rv))
-    return E_FAIL;
+  return S_OK;
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
-  return S_OK;
+  return E_FAIL;
 }
 
 STDMETHODIMP
 nsAccessibleRelationWrap::get_target(long aTargetIndex, IUnknown **aTarget)
 {
 __try {
   nsCOMPtr<nsIAccessible> accessible;
   nsresult rv = GetTarget(aTargetIndex, getter_AddRefs(accessible));
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
 
   nsCOMPtr<nsIWinAccessNode> winAccessNode(do_QueryInterface(accessible));
   if (!winAccessNode)
     return E_FAIL;
 
   void *instancePtr = NULL;
   rv = winAccessNode->QueryNativeInterface(IID_IUnknown, &instancePtr);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   *aTarget = static_cast<IUnknown*>(instancePtr);
-} __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
+  return S_OK;
 
-  return S_OK;
+} __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
+  return E_FAIL;
 }
 
 STDMETHODIMP
 nsAccessibleRelationWrap::get_targets(long aMaxTargets, IUnknown **aTarget,
                                       long *aNTargets)
 {
 __try {
   *aNTargets = 0;
 
   nsCOMPtr<nsIArray> targets;
   nsresult rv = GetTargets(getter_AddRefs(targets));
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   PRUint32 length = 0;
   rv = targets->GetLength(&length);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
+
+  if (length == 0)
+    return S_FALSE;
 
   PRUint32 count = length < PRUint32(aMaxTargets) ? length : aMaxTargets;
 
   PRUint32 index = 0;
   for (; index < count; index++) {
-    nsCOMPtr<nsIWinAccessNode> winAccessNode(do_QueryElementAt(targets, index, &rv));
-    if (NS_FAILED(rv) || !winAccessNode)
+    nsCOMPtr<nsIWinAccessNode> winAccessNode =
+      do_QueryElementAt(targets, index, &rv);
+    if (NS_FAILED(rv))
       break;
 
     void *instancePtr = NULL;
     nsresult rv =  winAccessNode->QueryNativeInterface(IID_IUnknown,
                                                        &instancePtr);
     if (NS_FAILED(rv))
       break;
 
     aTarget[index] = static_cast<IUnknown*>(instancePtr);
   }
 
   if (NS_FAILED(rv)) {
     for (PRUint32 index2 = 0; index2 < index; index2++) {
       aTarget[index2]->Release();
       aTarget[index2] = NULL;
     }
-    return E_FAIL;
+    return GetHRESULT(rv);
   }
 
   *aNTargets = count;
-} __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
+  return S_OK;
 
-  return S_OK;
+} __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
+  return E_FAIL;
 }
 
--- a/accessible/src/msaa/nsAccessibleWrap.cpp
+++ b/accessible/src/msaa/nsAccessibleWrap.cpp
@@ -1160,171 +1160,193 @@ nsAccessibleWrap::Reset(void)
 STDMETHODIMP
 nsAccessibleWrap::get_nRelations(long *aNRelations)
 {
 __try {
   PRUint32 count = 0;
   nsresult rv = GetRelationsCount(&count);
   *aNRelations = count;
 
-  if (NS_FAILED(rv))
-    return E_FAIL;
+  return GetHRESULT(rv);
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-  return S_OK;
+  return E_FAIL;
 }
 
 STDMETHODIMP
 nsAccessibleWrap::get_relation(long aRelationIndex,
                                IAccessibleRelation **aRelation)
 {
 __try {
+  *aRelation = NULL;
+
   nsCOMPtr<nsIAccessibleRelation> relation;
   nsresult rv = GetRelation(aRelationIndex, getter_AddRefs(relation));
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   nsCOMPtr<nsIWinAccessNode> winAccessNode(do_QueryInterface(relation));
   if (!winAccessNode)
     return E_FAIL;
 
   void *instancePtr = NULL;
   rv =  winAccessNode->QueryNativeInterface(IID_IAccessibleRelation,
                                             &instancePtr);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   *aRelation = static_cast<IAccessibleRelation*>(instancePtr);
+  return S_OK;
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-  return S_OK;
+  return E_FAIL;
 }
 
 STDMETHODIMP
 nsAccessibleWrap::get_relations(long aMaxRelations,
                                 IAccessibleRelation **aRelation,
                                 long *aNRelations)
 {
 __try {
+  *aRelation = NULL;
   *aNRelations = 0;
 
   nsCOMPtr<nsIArray> relations;
   nsresult rv = GetRelations(getter_AddRefs(relations));
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   PRUint32 length = 0;
   rv = relations->GetLength(&length);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
+
+  if (length == 0)
+    return S_FALSE;
 
   PRUint32 count = length < (PRUint32)aMaxRelations ? length : aMaxRelations;
 
   PRUint32 index = 0;
   for (; index < count; index++) {
-    nsCOMPtr<nsIWinAccessNode> winAccessNode(do_QueryElementAt(relations, index, &rv));
-    if (NS_FAILED(rv) || !winAccessNode)
+    nsCOMPtr<nsIWinAccessNode> winAccessNode =
+      do_QueryElementAt(relations, index, &rv);
+    if (NS_FAILED(rv))
       break;
 
     void *instancePtr = NULL;
     nsresult rv =  winAccessNode->QueryNativeInterface(IID_IAccessibleRelation,
                                                        &instancePtr);
     if (NS_FAILED(rv))
       break;
 
     aRelation[index] = static_cast<IAccessibleRelation*>(instancePtr);
   }
 
   if (NS_FAILED(rv)) {
     for (PRUint32 index2 = 0; index2 < index; index2++) {
       aRelation[index2]->Release();
       aRelation[index2] = NULL;
     }
-    return E_FAIL;
+    return GetHRESULT(rv);
   }
 
   *aNRelations = count;
+  return S_OK;
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-  return S_OK;
+  return E_FAIL;
 }
 
 STDMETHODIMP
-nsAccessibleWrap::role(long *role)
+nsAccessibleWrap::role(long *aRole)
 {
 __try {
+  *aRole = 0;
+
   PRUint32 xpRole = 0;
-  if (NS_FAILED(GetFinalRole(&xpRole)))
-    return E_FAIL;
+  nsresult rv = GetFinalRole(&xpRole);
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
 
   NS_ASSERTION(gWindowsRoleMap[nsIAccessibleRole::ROLE_LAST_ENTRY].ia2Role == ROLE_WINDOWS_LAST_ENTRY,
                "MSAA role map skewed");
 
-  *role = gWindowsRoleMap[xpRole].ia2Role;
+  *aRole = gWindowsRoleMap[xpRole].ia2Role;
+  return S_OK;
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-  return S_OK;
+  return E_FAIL;
 }
 
 STDMETHODIMP
 nsAccessibleWrap::scrollTo(enum IA2ScrollType aScrollType)
 {
 __try {
-  if (NS_SUCCEEDED(ScrollTo(aScrollType)))
-    return S_OK;
+  nsresult rv = ScrollTo(aScrollType);
+  return GetHRESULT(rv);
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
   return E_FAIL;
 }
 
 STDMETHODIMP
 nsAccessibleWrap::scrollToPoint(enum IA2CoordinateType aCoordType,
                                 long aX, long aY)
 {
 __try {
   PRUint32 geckoCoordType = (aCoordType == IA2_COORDTYPE_SCREEN_RELATIVE) ?
     nsIAccessibleCoordinateType::COORDTYPE_SCREEN_RELATIVE :
     nsIAccessibleCoordinateType::COORDTYPE_PARENT_RELATIVE;
 
-  return NS_SUCCEEDED(ScrollToPoint(geckoCoordType, aX, aY)) ?
-    S_OK : E_FAIL;
+  nsresult rv = ScrollToPoint(geckoCoordType, aX, aY);
+  return GetHRESULT(rv);
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-  return S_OK;
+  return E_FAIL;
 }
 
 STDMETHODIMP
 nsAccessibleWrap::get_groupPosition(long *aGroupLevel,
                                     long *aSimilarItemsInGroup,
                                     long *aPositionInGroup)
 {
 __try {
   PRInt32 groupLevel = 0;
   PRInt32 similarItemsInGroup = 0;
   PRInt32 positionInGroup = 0;
   nsresult rv = GroupPosition(&groupLevel, &similarItemsInGroup,
                               &positionInGroup);
 
-  if (NS_SUCCEEDED(rv)) {
-   *aGroupLevel = groupLevel;
-   *aSimilarItemsInGroup = similarItemsInGroup;
-   *aPositionInGroup = positionInGroup;
-    return S_OK;
-  }
+  *aGroupLevel = groupLevel;
+  *aSimilarItemsInGroup = similarItemsInGroup;
+  *aPositionInGroup = positionInGroup;
+
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
+
+  if (groupLevel ==0 && similarItemsInGroup == 0 && positionInGroup == 0)
+    return S_FALSE;
+  return S_OK;
 
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
   return E_FAIL;
 }
 
 STDMETHODIMP
 nsAccessibleWrap::get_states(AccessibleStates *aStates)
 {
 __try {
   *aStates = 0;
 
   // XXX: bug 344674 should come with better approach that we have here.
 
   PRUint32 states = 0, extraStates = 0;
   nsresult rv = GetFinalState(&states, &extraStates);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   if (states & nsIAccessibleStates::STATE_INVALID)
     *aStates |= IA2_STATE_INVALID_ENTRY;
   if (states & nsIAccessibleStates::STATE_REQUIRED)
     *aStates |= IA2_STATE_REQUIRED;
 
   // The following IA2 states are not supported by Gecko
   // IA2_STATE_ARMED
@@ -1354,18 +1376,20 @@ nsAccessibleWrap::get_states(AccessibleS
     *aStates |= IA2_STATE_STALE;
   if (extraStates & nsIAccessibleStates::EXT_STATE_SUPPORTS_AUTOCOMPLETION)
     *aStates |= IA2_STATE_SUPPORTS_AUTOCOMPLETION;
   if (extraStates & nsIAccessibleStates::EXT_STATE_TRANSIENT)
     *aStates |= IA2_STATE_TRANSIENT;
   if (extraStates & nsIAccessibleStates::EXT_STATE_VERTICAL)
     *aStates |= IA2_STATE_VERTICAL;
 
+  return S_OK;
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-  return S_OK;
+  return E_FAIL;
 }
 
 STDMETHODIMP
 nsAccessibleWrap::get_extendedRole(BSTR *aExtendedRole)
 {
 __try {
   *aExtendedRole = NULL;
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
@@ -1374,17 +1398,17 @@ nsAccessibleWrap::get_extendedRole(BSTR 
 }
 
 STDMETHODIMP
 nsAccessibleWrap::get_localizedExtendedRole(BSTR *aLocalizedExtendedRole)
 {
 __try {
   *aLocalizedExtendedRole = NULL;
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-  
+
   return E_NOTIMPL;
 }
 
 STDMETHODIMP
 nsAccessibleWrap::get_nExtendedStates(long *aNExtendedStates)
 {
 __try {
   *aNExtendedStates = 0;
@@ -1419,67 +1443,82 @@ nsAccessibleWrap::get_localizedExtendedS
   return E_NOTIMPL;
 }
 
 STDMETHODIMP
 nsAccessibleWrap::get_uniqueID(long *uniqueID)
 {
 __try {
   void *id = nsnull;
-  if (NS_SUCCEEDED(GetUniqueID(&id))) {
-    *uniqueID = - reinterpret_cast<long>(id);
-    return S_OK;
-  }
+  nsresult rv = GetUniqueID(&id);
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
+
+  *uniqueID = - reinterpret_cast<long>(id);
+  return S_OK;
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
   return E_FAIL;
 }
 
 STDMETHODIMP
-nsAccessibleWrap::get_windowHandle(HWND *windowHandle)
+nsAccessibleWrap::get_windowHandle(HWND *aWindowHandle)
 {
 __try {
-  *windowHandle = 0;
+  *aWindowHandle = 0;
+
   if (!mDOMNode)
     return E_FAIL;
+
   void *handle = nsnull;
   nsresult rv = GetOwnerWindow(&handle);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
-  *windowHandle = reinterpret_cast<HWND>(handle);
+  *aWindowHandle = reinterpret_cast<HWND>(handle);
+  return S_OK;
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-  return S_OK;
+  return E_FAIL;
 }
 
 STDMETHODIMP
-nsAccessibleWrap::get_indexInParent(long *indexInParent)
+nsAccessibleWrap::get_indexInParent(long *aIndexInParent)
 {
 __try {
-  PRInt32 index;
-  if (NS_SUCCEEDED(GetIndexInParent(&index))) {
-    *indexInParent = index;
-    return S_OK;
-  }
+  *aIndexInParent = -1;
+
+  PRInt32 index = -1;
+  nsresult rv = GetIndexInParent(&index);
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
+
+  if (index == -1)
+    return S_FALSE;
+
+  *aIndexInParent = index;
+  return S_OK;
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
   return E_FAIL;
 }
 
 STDMETHODIMP
 nsAccessibleWrap::get_locale(IA2Locale *aLocale)
 {
 __try {
   // Language codes consist of a primary code and a possibly empty series of
   // subcodes: language-code = primary-code ( "-" subcode )*
   // Two-letter primary codes are reserved for [ISO639] language abbreviations.
   // Any two-letter subcode is understood to be a [ISO3166] country code.
 
   nsAutoString lang;
   nsresult rv = GetLanguage(lang);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   // If primary code consists from two letters then expose it as language.
   PRInt32 offset = lang.FindChar('-', 0);
   if (offset == -1) {
     if (lang.Length() == 2) {
       aLocale->language = ::SysAllocString(lang.get());
       return S_OK;
     }
@@ -1497,31 +1536,34 @@ nsAccessibleWrap::get_locale(IA2Locale *
     } else if (offset == 5) {
       aLocale->country = ::SysAllocStringLen(lang.get() + 3, 2);
     }
   }
 
   // Expose as a string if primary code or subcode cannot point to language or
   // country abbreviations or if there are more than one subcode.
   aLocale->variant = ::SysAllocString(lang.get());
+  return S_OK;
+
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-  return S_OK;
+  return E_FAIL;
 }
 
 STDMETHODIMP
 nsAccessibleWrap::get_attributes(BSTR *aAttributes)
 {
   // The format is name:value;name:value; with \ for escaping these
   // characters ":;=,\".
 __try {
   *aAttributes = NULL;
 
   nsCOMPtr<nsIPersistentProperties> attributes;
-  if (NS_FAILED(GetAttributes(getter_AddRefs(attributes))))
-    return E_FAIL;
+  nsresult rv = GetAttributes(getter_AddRefs(attributes));
+  if (NS_FAILED(rv))
+    return GetHRESULT(rv);
 
   if (!attributes)
     return S_FALSE;
 
   nsCOMPtr<nsISimpleEnumerator> propEnum;
   attributes->Enumerate(getter_AddRefs(propEnum));
   if (!propEnum)
     return E_FAIL;
@@ -1535,47 +1577,51 @@ nsAccessibleWrap::get_attributes(BSTR *a
     nsCOMPtr<nsISupports> propSupports;
     propEnum->GetNext(getter_AddRefs(propSupports));
 
     nsCOMPtr<nsIPropertyElement> propElem(do_QueryInterface(propSupports));
     if (!propElem)
       return E_FAIL;
 
     nsCAutoString name;
-    if (NS_FAILED(propElem->GetKey(name)))
-      return E_FAIL;
+    rv = propElem->GetKey(name);
+    if (NS_FAILED(rv))
+      return GetHRESULT(rv);
 
     PRUint32 offset = 0;
     while ((offset = name.FindCharInSet(kCharsToEscape, offset)) != kNotFound) {
       name.Insert('\\', offset);
       offset += 2;
     }
 
     nsAutoString value;
-    if (NS_FAILED(propElem->GetValue(value)))
+    rv = propElem->GetValue(value);
+    if (NS_FAILED(rv))
       return E_FAIL;
 
     offset = 0;
     while ((offset = value.FindCharInSet(kCharsToEscape, offset)) != kNotFound) {
       value.Insert('\\', offset);
       offset += 2;
     }
 
     AppendUTF8toUTF16(name, strAttrs);
     strAttrs.Append(':');
     strAttrs.Append(value);
     strAttrs.Append(';');
   }
 
-  *aAttributes = ::SysAllocStringLen(strAttrs.get(), strAttrs.Length()); 
-  if (!*aAttributes)
-    return E_OUTOFMEMORY;
+  if (strAttrs.IsEmpty())
+    return S_FALSE;
+
+  *aAttributes = ::SysAllocStringLen(strAttrs.get(), strAttrs.Length());
+  return *aAttributes ? S_OK : E_OUTOFMEMORY;
 
 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-  return S_OK;
+  return E_FAIL;
 }
 
 STDMETHODIMP
 nsAccessibleWrap::Clone(IEnumVARIANT FAR* FAR* ppenum)
 {
 __try {
   // Clone could be bad, the cloned items aren't tracked for shutdown
   // Then again, as long as the client releases the items in time, we're okay
--- a/accessible/src/msaa/nsApplicationAccessibleWrap.cpp
+++ b/accessible/src/msaa/nsApplicationAccessibleWrap.cpp
@@ -75,81 +75,78 @@ nsApplicationAccessibleWrap::get_appName
   *aName = NULL;
 
   if (!sAppInfo)
     return E_FAIL;
 
   nsCAutoString cname;
   nsresult rv = sAppInfo->GetName(cname);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   if (cname.IsEmpty())
     return S_FALSE;
 
   NS_ConvertUTF8toUTF16 name(cname);
   *aName = ::SysAllocStringLen(name.get(), name.Length());
-  if (!*aName)
-    return E_OUTOFMEMORY;
+  return *aName ? S_OK : E_OUTOFMEMORY;
 
 } __except(FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
-  return S_OK;
+  return E_FAIL;
 }
 
 STDMETHODIMP
 nsApplicationAccessibleWrap::get_appVersion(BSTR *aVersion)
 {
 __try {
   *aVersion = NULL;
 
   if (!sAppInfo)
     return E_FAIL;
 
   nsCAutoString cversion;
   nsresult rv = sAppInfo->GetVersion(cversion);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   if (cversion.IsEmpty())
     return S_FALSE;
 
   NS_ConvertUTF8toUTF16 version(cversion);
   *aVersion = ::SysAllocStringLen(version.get(), version.Length());
-  if (!*aVersion)
-    return E_OUTOFMEMORY;
+  return *aVersion ? S_OK : E_OUTOFMEMORY;
 
 } __except(FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-  return S_OK;
+  return E_FAIL;
 }
 
 STDMETHODIMP
 nsApplicationAccessibleWrap::get_toolkitName(BSTR *aName)
 {
 __try {
   *aName = ::SysAllocString(L"Gecko");
   return *aName ? S_OK : E_OUTOFMEMORY;
+
 } __except(FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
-
   return E_FAIL;
 }
 
 STDMETHODIMP
 nsApplicationAccessibleWrap::get_toolkitVersion(BSTR *aVersion)
 {
 __try {
   *aVersion = NULL;
 
   if (!sAppInfo)
     return E_FAIL;
 
   nsCAutoString cversion;
   nsresult rv = sAppInfo->GetPlatformVersion(cversion);
   if (NS_FAILED(rv))
-    return E_FAIL;
+    return GetHRESULT(rv);
 
   if (cversion.IsEmpty())
     return S_FALSE;
 
   NS_ConvertUTF8toUTF16 version(cversion);
   *aVersion = ::SysAllocStringLen(version.get(), version.Length());
   return *aVersion ? S_OK : E_OUTOFMEMORY;