Handling Errors
Example 1 does not handle errors. Example 2 is an improved version that detects errors and prints a message.
NOTE: This section assumes that you understand C++ exceptions.
Example 2 – Handling errors
#include <rw/rstream.h>
#include <rw/network/RWSocketPortal.h>
#include <rw/network/RWInetAddr.h>
#include <rw/network/RWWinSockInfo.h>
 
int main()
{
RWWinSockInfo info;
try { //1
RWSocketPortal p( RWInetAddr(3010,"net.roguewave.com") );
p.sendAtLeast("Hello out there!");
}
catch (const RWxmsg& x) { //2
cerr << "Problem! " << x.why() << endl; //3
}
return 0;
}
//1 Encloses the main part of the example in a try block. If an exception is thrown from within the try block, the program exits the scope of the try block and looks for a catch clause to handle the exception.
//2 Catches all Networking package and SourcePro Core exceptions that can be thrown from within the try block (all Rogue Wave exceptions are derived from RWxmsg). For more information, see the .
//3 Handles the exception by printing an error message. The member function why() is part of the RWxmsg class interface and is available for any exception that the Networking package might throw.