Rogue Wave banner
Previous fileTop of DocumentContentsIndexNext file

8.3 Catching Exceptions

By default a stream does not throw any exceptions.6 You must explicitly activate an exception because a stream contains an exception mask. Each flag in this mask corresponds to one of the error flags. For example, once the badbit flag is set in the exception mask, an exception is thrown each time the badbit flag gets set in the stream state. The following code demonstrates how to activate an exception on an input stream InStr:

//1In calling the exceptions() function, you indicate what flags in the stream's state shall cause an exception to be thrown.7
//2Objects thrown by the stream's operations are of types derived from ios_base::failure. Hence this catch clause catches all stream exceptions in principle. We qualify this generalization because a stream might fail to catch certain exceptions like bad_alloc, for example, so that exceptions other than ios_base::failure might be raised. That's how exception handling in C++ works: you never know what exceptions will be raised.

Generally, it is a good idea to activate the badbit exception and suppress the eofbit and failbit exceptions, because the latter do not represent exceptional states. A badbit situation, however, is likely to be a serious error condition similar to the memory shortage indicated by a bad_alloc exception. Unless you want to suppress exceptions thrown by iostreams altogether, we would recommend that you switch on the badbit exception and turn off eofbit and failbit.



Previous fileTop of DocumentContentsIndexNext file

OEM Edition, OEM Edition, ©Copyright 1999, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.