TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: SEBASTIAN NOZZI
from: DARIN MCBRIDE
date: 1998-03-27 19:39:00
subject: Exceptions

 SN>  Que son exceptions? Que tienen que ver con C++? Para que sirven, como 
e
 SN>  manejan?
The sole languages of this echo are English and C++ (and related computer 
languages).  Thankfully you provided a translation...
 SN>  What are exceptions and what do they have to do with C++? What are they
 SN>  used for?
Exceptions are for "exceptional" returns.  It is a way of saying something 
weird happened, and "allowing" you to return funky objects rather than the 
one you said you would return.  The way that this is returned allows you to 
put all your error-catching/error-handling code in one area, rather than 
spread out over your entire program.  I don't personally use this feature of 
C++, though.
i.e.,
int ReadByteFromStream(istream& is)
{
  if (!is)
    throw StreamNotOpen(is); // assume this class is defined elsewhere.
  // ... other problems crop up, throw something else for each
  return ret_val;
}
try
{
  char c;
  while (EOF != (c = ReadByteFromStream(my_com_port)))
  {
    do_something(c);
  }
}
catch(StreamNotOpen& sno)
{
  cerr << "Reading from non-open comport" << endl;
  // handle this
}
catch(DoSomethingException1& dse)
{
  // handle do_something's exception #1
}
catch(DoSomethingException2& dse)
{
  // handle do_something's exception #2
}
// .. etc.
catch(...)
{
  // handle unknown error here
}
The main code is very compact.  I find that all the catches are somewhat 
counterproductive in this way, although in languages where exceptions are 
part of the language from the beginning, this can be much more efficient than 
the old C way of handling errors where they occur or manually passing them up 
the stack.  Note that if do_something called another function that threw an 
exception, and didn't catch the exception itself in do_something, we could 
catch it up here, too, allowing yet more error handling code to be in only 
one place.
Hope this helps,
Darin McBride
C_PLUSPLUS moderator
---
---------------
* Origin: Tanktalus' Tower BBS (1:250/102)

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