TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: DARIN MCBRIDE
from: CAMERON CLARK
date: 1997-07-27 18:53:00
subject: Re: exceptions in c++

DM> void GetToken(
DM>     if (is)
DM>       if (isspace(c))
DM>   } while (token.length() < MAX_TOKEN_LEN);
    You've got it down to 3(logical) check per character.
    Here's what I eventually came up with:
void Token::getVariable() {
  char c;
  do {
    inf->get(c);
    if (inf->gcount() != 1) break;
    if (cur>=30)            break;
    if (!isMyChar(c))       {inf->putback(c);break;};
    token[cur++] = c;
  } while (1);
}
    It's heavily unstructured, yet more readable than any structured rule
    of thumb I could come up with. My goals are usually "easily readable"
    over most others.
    Here's the unrefined "eatWhite" procedure:
void Token::eatWhite() {
  // loop while there's a space (or space creating character)
  char c;
  do {
    inf->get(c);
    if (c == '\n') linenum++;      // keep track of new lines
  } while (strContains(" \t\n",c) &&
           (inf->gcount() ==1));
  if (inf->gcount() == 1) inf->putback(c);
}
    Re-written could look like
  char c;
  do {
    inf->get(c);
    if (inf->gcount() == 0)  break;
    if (!strContains(" \t\n",c))  {inf->putback(c); break;}
  while (1);
DM> Which is why it needs to be put into its own function.  (I, however, 
on't
DM> have a problem with multiple exits, especially where they are quite 
useful.
    Agreed. I use a rule of thumb scale to determine when multiple exists
    and infinite loops are better - yes, better - than any other way.
    I shy away from bad practices where applicable.
DM> Generally I/O is slower than CPU-bound activity anyway.  Whereever you're
DM> reading from (whether disk, floppy, modem, named pipe, whatever), three
DM> checks will likely be faster than the average is.getc() call.
    I just need to know how to override the "get()" call to throw and
    exception.
    myifstream::get(char& c) {
      ifstream::get(c);
      if (ifstream::gcount() == 0)  "throw EOF exception";
    }
DM> You're doing a check every character, every time, as it is - the if that
DM> determines whether or not to throw.  My code does no different, but 
without
DM> the overhead of a throw.
    Yes. I can't get around doing at the very least one check per character
    read, but, using one piece of code I elmininate allot of debugging and
    error checking each time I need to get a certain type of token.
DM> Perhaps you do - but, if so, it's gotta be for more than just this.
    Java has the tendency to force you to error check your code. Certain
    exceptions cannot be avoided and requires you to at the very least
    use the "try,catch" structure even if it's empty.
    try {
      "IOcall"
    } catch IOException {
      "error reporting goes here"
    }
    Even though tedious, it's good practice. For mickey mouse programs that
    just need a brute force method to get a small task done, it's a pain.
    I think I would only use the override stream for special cases.
--- GEcho 1.00
---------------
* Origin: Digital OnLine Magazine! - (409)838-8237 (1:3811/350)

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™.