The client program above is just what you need when things are going well. In the real world, though, things go wrong. Networks crash, operating systems run out of socket resources, and servers aren't always available. Applications written with the net library use C++ exceptions to handle such problems.
NOTE: The remainder of this section assumes you understand C++ exceptions. If you are unfamiliar with exceptions, you should consult a good C++ manual to understand the topic. Several possibilities are listed in the bibliography.
Here is an improved version of our example program. This one detects problems and prints an error message.
#include <rw/rstream.h> #include <rw/toolpro/sockport.h> #include <rw/toolpro/inetaddr.h> #include <rw/toolpro/winsock.h> 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; }
Here is what we added:
//1 | The guts of the example are now enclosed in a try block. If an exception gets thrown from within the try block, the program exits the scope of the try block and tries to find a suitable catch clause to handle the exception. |
//2 | This catch clause catches all of the net library (and Tools.h++) exceptions that could be thrown from within the try block, since all Rogue Wave exceptions are derived from RWxmsg (see the "Errors" section in your Tools.h++ User's Guide for details). |
//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 net library might throw. |
If you had trouble getting the first example program to run correctly, this example may shed some light on the problem by printing an error message. If you still can't get things running, even with the error message, you may want to ask your local network expert for help.
©Copyright 2000, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.