From 958647601758a9cbd78082a76c3583c3ce9e852e Mon Sep 17 00:00:00 2001 From: "Charles E. Rolke" Date: Tue, 2 Nov 2010 20:50:54 +0000 Subject: QPID-2923 Qpid Messaging .NET Binding fails to translate exceptions from C++ to .NET This checkin moves code out of class constructor member initialization and puts it into try-catch blocks. Any SEH Exceptions thrown by the C++ Messaging libraries are caught and re-thrown as .NET exceptions. git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@1030209 13f79535-47bb-0310-9956-ffa450edef68 --- cpp/bindings/qpid/dotnet/src/Connection.cpp | 142 ++++++++++++++++++++++++---- 1 file changed, 124 insertions(+), 18 deletions(-) (limited to 'cpp/bindings/qpid/dotnet/src/Connection.cpp') diff --git a/cpp/bindings/qpid/dotnet/src/Connection.cpp b/cpp/bindings/qpid/dotnet/src/Connection.cpp index 322910d820..d0c5bc9ac5 100644 --- a/cpp/bindings/qpid/dotnet/src/Connection.cpp +++ b/cpp/bindings/qpid/dotnet/src/Connection.cpp @@ -43,36 +43,97 @@ namespace Messaging { /// // constructors - Connection::Connection(System::String ^ url) : - connectionp(new ::qpid::messaging::Connection(QpidMarshal::ToNative(url))) + Connection::Connection(System::String ^ url) { + System::Exception ^ newException = nullptr; + + try + { + connectionp = new ::qpid::messaging::Connection(QpidMarshal::ToNative(url)); + } + catch (const ::qpid::types::Exception & error) + { + String ^ errmsg = gcnew String(error.what()); + newException = gcnew QpidException(errmsg); + } + + if (newException != nullptr) + { + throw newException; + } } Connection::Connection(System::String ^ url, System::Collections::Generic::Dictionary< - System::String ^, System::Object ^> ^ options) : - connectionp(new ::qpid::messaging::Connection(QpidMarshal::ToNative(url))) + System::String ^, System::Object ^> ^ options) { - for each (System::Collections::Generic::KeyValuePair kvp in options) - { - SetOption(kvp.Key, kvp.Value); + System::Exception ^ newException = nullptr; + + try + { + connectionp = new ::qpid::messaging::Connection(QpidMarshal::ToNative(url)); + + for each (System::Collections::Generic::KeyValuePair kvp in options) + { + SetOption(kvp.Key, kvp.Value); + } + } + catch (const ::qpid::types::Exception & error) + { + String ^ errmsg = gcnew String(error.what()); + newException = gcnew QpidException(errmsg); } + + if (newException != nullptr) + { + throw newException; + } } - Connection::Connection(System::String ^ url, System::String ^ options) : - connectionp(new ::qpid::messaging::Connection(QpidMarshal::ToNative(url), - QpidMarshal::ToNative(options))) + Connection::Connection(System::String ^ url, System::String ^ options) { + System::Exception ^ newException = nullptr; + + try + { + connectionp = new ::qpid::messaging::Connection(QpidMarshal::ToNative(url), + QpidMarshal::ToNative(options)); + } + catch (const ::qpid::types::Exception & error) + { + String ^ errmsg = gcnew String(error.what()); + newException = gcnew QpidException(errmsg); + } + + if (newException != nullptr) + { + throw newException; + } } // Copy constructor Connection::Connection(const Connection ^ connection) - : connectionp(new ::qpid::messaging::Connection( - *(const_cast(connection)->NativeConnection))) { + System::Exception ^ newException = nullptr; + + try + { + connectionp = new ::qpid::messaging::Connection( + *(const_cast(connection)->NativeConnection)); + } + catch (const ::qpid::types::Exception & error) + { + String ^ errmsg = gcnew String(error.what()); + newException = gcnew QpidException(errmsg); + } + + if (newException != nullptr) + { + throw newException; + } } @@ -104,20 +165,65 @@ namespace Messaging { void Connection::SetOption(System::String ^ name, System::Object ^ value) { - ::qpid::types::Variant entryValue; - TypeTranslator::ManagedToNativeObject(value, entryValue); - std::string entryName = QpidMarshal::ToNative(name); - connectionp->::qpid::messaging::Connection::setOption(entryName, entryValue); + System::Exception ^ newException = nullptr; + + try + { + ::qpid::types::Variant entryValue; + TypeTranslator::ManagedToNativeObject(value, entryValue); + std::string entryName = QpidMarshal::ToNative(name); + connectionp->::qpid::messaging::Connection::setOption(entryName, entryValue); + } + catch (const ::qpid::types::Exception & error) + { + String ^ errmsg = gcnew String(error.what()); + newException = gcnew QpidException(errmsg); + } + + if (newException != nullptr) + { + throw newException; + } } void Connection::Open() { - connectionp->open(); + System::Exception ^ newException = nullptr; + + try + { + connectionp->open(); + } + catch (const ::qpid::types::Exception & error) + { + String ^ errmsg = gcnew String(error.what()); + newException = gcnew QpidException(errmsg); + } + + if (newException != nullptr) + { + throw newException; + } } void Connection::Close() { - connectionp->close(); + System::Exception ^ newException = nullptr; + + try + { + connectionp->close(); + } + catch (const ::qpid::types::Exception & error) + { + String ^ errmsg = gcnew String(error.what()); + newException = gcnew QpidException(errmsg); + } + + if (newException != nullptr) + { + throw newException; + } } // -- cgit v1.2.1