From 3a51daeace695ead38cfd82b3a0f1e6f25a32e0f Mon Sep 17 00:00:00 2001 From: Noel Grandin Date: Thu, 24 May 2018 15:47:30 +0200 Subject: Improve re-throwing of UNO exceptions (*) if we are already throwing a Wrapped*Exception, get the exception using cppu::getCaughtexception. (*) when catching and then immediately throwing UNO exceptions, use cppu::getCaughtException to prevent exception slicing (*) if we are going to catch an exception and then immediately throw a RuntimeException, rather throw a WrappedTargetRuntimeException and preserve the original exception information. Change-Id: Ia7a501a50ae0e6f4d05186333c8517fdcb17d558 Reviewed-on: https://gerrit.libreoffice.org/54692 Tested-by: Jenkins Reviewed-by: Noel Grandin --- cppuhelper/source/component.cxx | 8 ++- cppuhelper/source/implbase.cxx | 14 +++-- cppuhelper/source/propertysetmixin.cxx | 106 +++++++++++++++++++-------------- 3 files changed, 76 insertions(+), 52 deletions(-) (limited to 'cppuhelper') diff --git a/cppuhelper/source/component.cxx b/cppuhelper/source/component.cxx index d906243bfddb..16cf56f257e6 100644 --- a/cppuhelper/source/component.cxx +++ b/cppuhelper/source/component.cxx @@ -20,8 +20,10 @@ #include #include #include +#include #include #include +#include #include using namespace osl; @@ -189,8 +191,10 @@ void OComponentHelper::dispose() } catch (Exception & exc) { - throw RuntimeException( - "unexpected UNO exception caught: " + exc.Message ); + css::uno::Any anyEx = cppu::getCaughtException(); + throw lang::WrappedTargetRuntimeException( + "unexpected UNO exception caught: " + exc.Message, + nullptr, anyEx ); } } else diff --git a/cppuhelper/source/implbase.cxx b/cppuhelper/source/implbase.cxx index b8bc91b769b1..f16c587431f0 100644 --- a/cppuhelper/source/implbase.cxx +++ b/cppuhelper/source/implbase.cxx @@ -18,11 +18,13 @@ */ #include +#include #include #include #include #include +#include #include using namespace ::osl; @@ -120,8 +122,10 @@ void WeakComponentImplHelperBase::dispose() } catch (Exception & exc) { - throw RuntimeException( - "unexpected UNO exception caught: " + exc.Message ); + css::uno::Any anyEx = cppu::getCaughtException(); + throw lang::WrappedTargetRuntimeException( + "unexpected UNO exception caught: " + exc.Message, + nullptr, anyEx ); } } } @@ -244,8 +248,10 @@ void WeakAggComponentImplHelperBase::dispose() } catch (Exception & exc) { - throw RuntimeException( - "unexpected UNO exception caught: " + exc.Message ); + css::uno::Any anyEx = cppu::getCaughtException(); + throw lang::WrappedTargetRuntimeException( + "unexpected UNO exception caught: " + exc.Message, + nullptr, anyEx ); } } } diff --git a/cppuhelper/source/propertysetmixin.cxx b/cppuhelper/source/propertysetmixin.cxx index 8469f1a7c51d..88e104d90453 100644 --- a/cppuhelper/source/propertysetmixin.cxx +++ b/cppuhelper/source/propertysetmixin.cxx @@ -66,6 +66,7 @@ #include #include #include +#include #include #include #include @@ -436,9 +437,11 @@ PropertySetMixinImpl::Impl::Impl( m_type.getTypeName()), css::uno::UNO_QUERY_THROW); } catch (css::container::NoSuchElementException & e) { - throw css::uno::RuntimeException( + css::uno::Any anyEx = cppu::getCaughtException(); + throw css::lang::WrappedTargetRuntimeException( "unexpected com.sun.star.container.NoSuchElementException: " - + e.Message); + + e.Message, + nullptr, anyEx ); } std::vector< rtl::OUString > handleNames; initProperties(ifc, m_absentOptional, &handleNames); @@ -509,10 +512,11 @@ void PropertySetMixinImpl::Impl::setProperty( throw css::lang::IllegalArgumentException( e.Message, object, illegalArgumentPosition); } else { - throw css::uno::RuntimeException( - ("unexpected com.sun.star.lang.IllegalArgumentException: " - + e.Message), - object); + css::uno::Any anyEx = cppu::getCaughtException(); + throw css::lang::WrappedTargetRuntimeException( + "unexpected com.sun.star.lang.IllegalArgumentException: " + + e.Message, + object, anyEx ); } } catch (css::lang::IllegalAccessException &) { //TODO Clarify whether PropertyVetoException is the correct exception @@ -564,10 +568,11 @@ css::uno::Any PropertySetMixinImpl::Impl::getProperty( try { value = field->get(object->queryInterface(m_type)); } catch (css::lang::IllegalArgumentException & e) { - throw css::uno::RuntimeException( - ("unexpected com.sun.star.lang.IllegalArgumentException: " - + e.Message), - object); + css::uno::Any anyEx = cppu::getCaughtException(); + throw css::lang::WrappedTargetRuntimeException( + "unexpected com.sun.star.lang.IllegalArgumentException: " + + e.Message, + object, anyEx ); } catch (css::lang::WrappedTargetRuntimeException & e) { //FIXME A WrappedTargetRuntimeException from XIdlField2.get is not // guaranteed to originate directly within XIdlField2.get (and thus have @@ -621,10 +626,11 @@ css::uno::Any PropertySetMixinImpl::Impl::getProperty( ambiguous->getField("Value"), css::uno::UNO_QUERY_THROW)-> get(value); } catch (css::lang::IllegalArgumentException & e) { - throw css::uno::RuntimeException( - ("unexpected com.sun.star.lang.IllegalArgumentException: " - + e.Message), - object); + css::uno::Any anyEx = cppu::getCaughtException(); + throw css::lang::WrappedTargetRuntimeException( + "unexpected com.sun.star.lang.IllegalArgumentException: " + + e.Message, + object, anyEx ); } undoAmbiguous = false; } else if (undoDefaulted @@ -649,10 +655,11 @@ css::uno::Any PropertySetMixinImpl::Impl::getProperty( defaulted->getField("Value"), css::uno::UNO_QUERY_THROW)-> get(value); } catch (css::lang::IllegalArgumentException & e) { - throw css::uno::RuntimeException( - ("unexpected com.sun.star.lang.IllegalArgumentException: " - + e.Message), - object); + css::uno::Any anyEx = cppu::getCaughtException(); + throw css::lang::WrappedTargetRuntimeException( + "unexpected com.sun.star.lang.IllegalArgumentException: " + + e.Message, + object, anyEx ); } undoDefaulted = false; } else if (undoOptional @@ -681,10 +688,11 @@ css::uno::Any PropertySetMixinImpl::Impl::getProperty( optional->getField("Value"), css::uno::UNO_QUERY_THROW)-> get(value); } catch (css::lang::IllegalArgumentException & e) { - throw css::uno::RuntimeException( - ("unexpected com.sun.star.lang.IllegalArgumentException: " - + e.Message), - object); + css::uno::Any anyEx = cppu::getCaughtException(); + throw css::lang::WrappedTargetRuntimeException( + "unexpected com.sun.star.lang.IllegalArgumentException: " + + e.Message, + object, anyEx ); } undoOptional = false; } else { @@ -738,15 +746,17 @@ css::uno::Any PropertySetMixinImpl::Impl::wrapValue( type->getField("IsAmbiguous"), css::uno::UNO_QUERY_THROW)->set( strct, css::uno::Any(isAmbiguous)); } catch (css::lang::IllegalArgumentException & e) { - throw css::uno::RuntimeException( - ("unexpected com.sun.star.lang.IllegalArgumentException: " - + e.Message), - object); + css::uno::Any anyEx = cppu::getCaughtException(); + throw css::lang::WrappedTargetRuntimeException( + "unexpected com.sun.star.lang.IllegalArgumentException: " + + e.Message, + object, anyEx ); } catch (css::lang::IllegalAccessException & e) { - throw css::uno::RuntimeException( - ("unexpected com.sun.star.lang.IllegalAccessException: " - + e.Message), - object); + css::uno::Any anyEx = cppu::getCaughtException(); + throw css::lang::WrappedTargetRuntimeException( + "unexpected com.sun.star.lang.IllegalAccessException: " + + e.Message, + object, anyEx ); } return strct; } @@ -767,15 +777,17 @@ css::uno::Any PropertySetMixinImpl::Impl::wrapValue( type->getField("IsDefaulted"), css::uno::UNO_QUERY_THROW)->set( strct, css::uno::Any(isDefaulted)); } catch (css::lang::IllegalArgumentException & e) { - throw css::uno::RuntimeException( - ("unexpected com.sun.star.lang.IllegalArgumentException: " - + e.Message), - object); + css::uno::Any anyEx = cppu::getCaughtException(); + throw css::lang::WrappedTargetRuntimeException( + "unexpected com.sun.star.lang.IllegalArgumentException: " + + e.Message, + object, anyEx ); } catch (css::lang::IllegalAccessException & e) { - throw css::uno::RuntimeException( - ("unexpected com.sun.star.lang.IllegalAccessException: " - + e.Message), - object); + css::uno::Any anyEx = cppu::getCaughtException(); + throw css::lang::WrappedTargetRuntimeException( + "unexpected com.sun.star.lang.IllegalAccessException: " + + e.Message, + object, anyEx ); } return strct; } @@ -799,15 +811,17 @@ css::uno::Any PropertySetMixinImpl::Impl::wrapValue( isAmbiguous, wrapDefaulted, isDefaulted, false)); } } catch (css::lang::IllegalArgumentException & e) { - throw css::uno::RuntimeException( - ("unexpected com.sun.star.lang.IllegalArgumentException: " - + e.Message), - object); + css::uno::Any anyEx = cppu::getCaughtException(); + throw css::lang::WrappedTargetRuntimeException( + "unexpected com.sun.star.lang.IllegalArgumentException: " + + e.Message, + object, anyEx ); } catch (css::lang::IllegalAccessException & e) { - throw css::uno::RuntimeException( - ("unexpected com.sun.star.lang.IllegalAccessException: " - + e.Message), - object); + css::uno::Any anyEx = cppu::getCaughtException(); + throw css::lang::WrappedTargetRuntimeException( + "unexpected com.sun.star.lang.IllegalAccessException: " + + e.Message, + object, anyEx ); } return strct; } -- cgit