Try/catch (or __try __except) functions for intercept exceptions

Questions regarding the use of the C++ SDK for Server or Client development or integration into customer products ...

Moderator: uasdkcpp

Post Reply
Alex76Esa
Jr. Member
Jr. Member
Posts: 1
Joined: 16 Oct 2023, 14:46

Try/catch (or __try __except) functions for intercept exceptions

Post by Alex76Esa »

Hello,
I would like to use a __try __except functions to prevent an exception if something goes wrong when calling "addNodeAndReference" method of NodeManagerBase derived class.

Code: Select all

UaStatus NodeManagerESA::AddRef(OpcUa::FolderType* pFatherFolder, OpcUa::FolderType* pChildFolder, UI32 j)
{
	UaStatus ret = OpcUa_Bad;

	__try
	{
		if (j == 0)
			ret = addNodeAndReference(OpcUaId_ObjectsFolder, pChildFolder, OpcUaId_Organizes);
		else
			ret = addNodeAndReference(pFatherFolder, pChildFolder, OpcUaId_Organizes);
	}
	__except (true)
	{
		ret = OpcUa_Bad;
	}

	return ret;
}
When I try to compile this code, an error will be generated:
"error C2712: Cannot use __try in functions that require object unwinding"

Can someone explain to me the correct way for prevent fatal errors and handle exceptions?
Thanks
Alessandro

User avatar
Support Team
Hero Member
Hero Member
Posts: 3072
Joined: 18 Mar 2011, 15:09

Re: Try/catch (or __try __except) functions for intercept exceptions

Post by Support Team »

Hello Alex,

the SDK does not throw exceptions. All you need to do is check the UaStatus addNodeAndReference returns.
Best regards
Unified Automation Support Team

Post Reply