TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: FRANK MASINGILL
from: KURT KUZBA
date: 1997-11-30 00:53:00
subject: Try blocks

FM>   The books says that unless my compiler "try" it cannot be
FM>   compiled.  I have Turbo C/C++ 3.0 from Borland.  It does
FM>   not have it.  What version would I have to get to have
FM>   that and how important is it?
   Just to be certain, type "try" all by itself and see if it
   shows the same contextual screen attributes as other
   reserved words in the editor, such as switch or else.
   The keyword try is used with catch, normally.
   Remember how we always tested a pointer when using it with
   malloc() in C? This is similar, where we are using new and
   delete instead of malloc() and free().
   C++ will throw an exception when an allocation fails.
   You can take advantage of this by using try and catch.
      try
      {
         PlayerClass *Player = new PlayerClass;
         return Player;
      }
      catch
      {
         cout << "I'm sorry, There is insufficient memory to\n"
                 "handle an additional players at this time.\n"
                 "Please try again later.\n" << flush;
         return PlayerClass(NULL);
      }
   This allows you to react to an allocation error, just as you
   would with a malloc():
      PlayerClass *Players = malloc(sizeof(PlayerClass));
      if(!Players)
      {
         printf("Malloc error in Players.\n");
      }
   The idea, of course, is that you have control of the
   situation when an allocation fails, and are able to do
   whatever is necessary to either salvage the situation or
   exit gracefully from your application.
   I'm sure somebody else can explain it better, and will. :)
---
> ] Win'95 :: The Commodore 128 of the Intel World..............
---
---------------
* Origin: *YOPS ]I[* 3.1 GIG * RA/FD/FE RADist * Milwaukee, WI (1:154/750)

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