TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: CAMERON CLARK
from: THOMAS MAEDER
date: 1997-07-20 13:26:00
subject: exceptions in c++

CC>     I've a little experience with java style exceptions, but none wi
CC>     I need to alter the ifstream to throw an exception once an end o
CC>     file is reached.
CC>     EG.
CC>     MYifstream.get(char c) {
CC>       ifstream::get(c);
CC>       if (ifstream::gcount()==0) {
CC>          "thow end of file exception"
CC>       }
CC>     }
Don't do this! An input stream isn't supposed to throw an exception on
end  of  file.  There  is  no  good  reason for it, since eof isn't an
exceptional situation.
Rather rewrite
CC>     try {
CC>     do { ifs.get(c); } while (c==' ');
CC>     } catch {
CC>       "end of file" exception
CC>     } finally {
CC>       ...
CC>     }
to
char c; 
while (ifs.get(c) && c!=' '); 
if (ifs.eof())
    // do your 'exceptional' stuff here
else
    // do your 'normal' stuff here
Thomas
---
 þ MM 1.0 #0113 þ I couldn't repair your brakes, so I made your horn louder.
---------------
* Origin: McMeier & Son BBS (2:301/138)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.