Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!rutgers!cs.utexas.edu!csd4.csd.uwm.edu!bionet!agate!ucbvax!hplabs!well!nagle From: nagle@well.UUCP (John Nagle) Newsgroups: comp.lang.c++ Subject: Handling trouble in C++ Keywords: exception error Message-ID: <13262@well.UUCP> Date: 22 Aug 89 06:39:59 GMT Distribution: comp Lines: 34 In C++, unlike C, a considerable fraction of the execution can take place implicitly, within constructors and destructors, rather than in explicitly called functions. But there is no way for a constructor or a destructor to signal an error condition to the invoker. Declarations aren't allowed to fail. But constructors often do such things as open files, windows, devices, or databases. Such operations can fail. What then? At present, we have a few options: 1. Panic and abort the program. This is the Pascal solution, and is one of the reasons nobody does serious work in standard Pascal. 2. Force the caller to do the opening operation separately, then pass a handle for the open object to the class, rather than letting the class handle the open itself. This is the solution chosen for the streams package. One of the major advantages of class declarations, the automatic execution of the destructor upon scope exit, is lost with this approach. 3. If the open fails, put the class instance into a "bad" state, so that later operations on it produce some error condition. This works, but provides no guarantee that the proper check will be made later. Even if it is, special effort must be made to save the information that describes what went wrong during construction and then to retrieve and analyze it later. Well, that's where we are today. What should be done? John Nagle