🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #2065

try blocks & compound statements

#2065
From: Tim
Date: Thu, 28 Sep 1995 17:19
80 lines
2101 bytes
Greetings,
 
I am curious as to why the try block has the syntax of:
 
	try <compound-statement> <handler-list>
 
 
instead of:
 
	try <statement> <handler-list>
 
 
The specific context which prompted me to investigate this issue is
that of a constructor which can throw an exception:
 
 
	class CFoo {
 
		class FooError { };
		
		CFoo(int bar) { if (bar>4) throw(FooError); }
	}
 
	...
 
	{
 
	    try {
	        CFoo x(1);
 
	        // x is destructed here!
	    }
	    
	    catch (FooError) {
	    ...
	    }
 
	    x.SomeMember(); // error: x is out of scope here
	}
 
 
Note that because the compount statement is required, x is created
and destroyed within the try block.
 
If the try block could be written without the compound statement, i.e.
 
	try CFoo x(1);
 
	catch (FooError) {
	    ...
	}
 
	x.SomeMember(); // ok, x is still in scope
	
 
then this scoping issue does not arise.
 
I realize that this can be worked-around using a larger try block that
encompasses the entire life-span of the constructed object, or by using
"new" and a pointer that exists before the try block.  Both of these
solutions seem less palatable in some situations than using the single
statement.
 
comments welcome!
 
regards,
 
- Tim

-- 
--
 Tim Zickus                         "On my fourth day of telecommuting,
 zickus@eecis.udel.edu               I realized that clothes are totally
 http://www.eecis.udel.edu/~zickus   unnecessary"                - Dilbert  

---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID: <MATT.95Sep28101903@physics2.Berkeley.EDU>
Path: arc1-ca.pugleaf.net!128.32.155.1!matt

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!128.32.155.1!matt
From: Tim <zickus@eecis.udel.edu>
Newsgroups: comp.std.c++
Subject: try blocks & compound statements
Date: 28 Sep 1995 17:19:03 GMT
Organization: <none>
Approved: matt@physics2.Berkeley.EDU (Matt Austern)
Message-ID: <MATT.95Sep28101903@physics2.Berkeley.EDU>
NNTP-Posting-Host: physics2.berkeley.edu