PTypes: streams: error handling

Top: Streams: Error handling

When a recoverable error occurs during input/output operations, an exception of type (estream*) is thrown. The exception object contains an error message which can be shown to the user. The message is accessible through estream::get_message().

The example below shows how to catch an exception and recover normal execution of the program:

>     #include <pstreams.h>

>     USING_PTYPES

>     infile f("somefile.txt");
>     try
>     {
>         f.open();
>         while(!f.get_eof())
>             // read the file...
>     }
>     catch (estream* e)
>     {
>         perr.putline(e->get_message());
>         delete e;
>     }

The estream class provides a system error code in UNIX errno semantics through estream::get_code(). On Windows most error codes are translated into corresponding UNIX errno codes. Less frequently occurring errors are translated to a generic errno code EIO. When an estream object returns EIO, the actual message string is constructed based on the status code of the stream. For example, if the status code was IO_OPENING, the error code was EIO, the message would be "Couldn't open [filename]". For explanations on what each errno code means, see comments in src/piobase.cxx source file.

Estream also provides a reference to the stream object that raised the error condition through estream::get_errstm().

See also: iobase, Examples

PTypes home