TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: SEBASTIAN NOZZI
from: CHRISTOPHER BUTLER
date: 1998-03-29 11:23:00
subject: Exceptions

Hi Sebastian!
Hey, Sebastian wasn't it you that was talking about Exceptions on 23 Mar 98? 
Yeah... you were talking to All, weren't you?
 SN>  What are exceptions and what do they have to do with C++? What are
 SN> they used for?
Exceptions are a form of error handling, they are (in my experience anyway), 
simple classes, defined a bit like this:
class GeneralError {
 public:
  GeneralError(int err): error(err) { }
  int errNo() { return err; }
  char *strError() { return strerror(err); }
 private:
  int error;
};
When an error occurs, the function where it happens will throw the exception:
char *myFunc()
{
  // ...
  if (something went wrong)
    throw GeneralError(5);
}
Then, in the calling function:
try {
  foo = myFunc();
} catch (xGeneralError err)  { cerr << err.strError() << endl; }
In C, without exceptions, the same function might go like this:
char *myFunc()
{
  /* ... */
  if (something went wrong)
    return NULL;
}
If this isn't handled in the calling function, then it can leed to all kinds 
of mayhem (segmentation faults, etc). However, if an exception is not caught, 
the program calls the abort() function, and exits cleanly.
-----------------------------------------------------------------------------
Chris Butler                               [email: chrisb@sandy.force9.co.uk]
-----------------------------------------------------------------------------
 I don't think anyone would ever say "A shower will scare you to death
 before it kills you."
 -- Abby Franquemont-Guillory on alt.sysadmin.recovery
--- FMailX32 1.22
---------------
* Origin: ... The Death Butler BBS, +44-1582-620141 ... (2:257/135)

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