Thread: try-catch-throw

  1. #1
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    try-catch-throw

    I don't really understand the advantages with the try-catch-throw statements. I took a look at msdn explanation of the statements and here's an example they've given:

    Code:
    int main() {
       char *buf;
       try {
          buf = new char[512];
          if( buf == 0 )
             throw "Memory allocation failure!";
       }
       catch( char * str ) {
          cout << "Exception raised: " << str << '\n';
       }
    }
    If I would have done the same thing I would have written:

    Code:
    int main() {
       char *buf;
       buf = new char[512];
       if (buf == 0) {
          cout << "Memory allocation failure!\n";
          cout << "Exception raised: " << str << '\n';
       }
    }
    I don't really se the difference and my is much more compact
    Last edited by TriKri; 07-03-2007 at 03:34 PM.
    Come on, you can do it! b( ~_')

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Yeah. They repeat that kind of stuff on tutorials all over the web.
    The hint is: forget about that throw.

    Here's something a little better. http://www.gamedev.net/reference/art...article953.asp

    Just keep reading past the first code you see.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    The advantage is that exceptions automates the practice of bubbling up boolean returns. For example, this might be exception-less error checking code.
    Code:
    // Reads a person's age from the string database
    // Returns 0 on error and sets errno
    long getAge(char const * name) {
       char const * age_str;
       long age_num;
    
       age_str = database_lookup (name);
       if (errno) // Database lookup error
          return 0;
    
       if (age_str == NULL)
       { // Can't read a null value.
          errno = 1;
          return 0;
       }
    
       char * endptr;
       age_num = strtol (age_str, &endptr, 10);
    
       if (errno) // Range error
          return 0;
    
       if (*endptr != 0) // Didn't read the whole string, it's not an int
       {
          errno = 1;
          return 0;
       }
          
       return age_num;
    }
    The hope with exceptions is that we could change that to something like this...
    Code:
    // Reads a person's age from the string database
    // Throws on err.
    long getAge(char const * name) {
       char const * age_str = database_lookup (name);
    
       if (age_str == NULL)
       { // Can't read a null value.
          std::ostringstream err;
          err << "DB field USERS.AGE_STR contains NULL field for USERS.NAME = " << name;
          throw database_error (err.str());
       }
    
       return boost::lexical_cast<long>(age_str);
    }
    The second snippet handles all the same errors in all the same places, it's just all done under the hood.


    The following links goe pretty into "Why exceptions are great" and how to properly use them, albeit light on examples.

    http://www.parashift.com/c++-faq-lite/exceptions.html
    http://www.scottcollins.net/articles/exceptions.html
    Callou collei we'll code the way
    Of prime numbers and pings!

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Quote Originally Posted by TriKri View Post
    Code:
    int main() {
       char *buf;
       buf = new char[512];
       if (buf == 0) {
          cout << "Memory allocation failure!\n";
          cout << "Exception raised: " << str << '\n';
       }
    }
    I don't really se the difference and my is much more compact
    compact and wrong. to make this do what you expect you'll have to use
    Code:
    buf = new(std::nothrow) char[512];

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    It doesn't help that the msdn example is out of date to the point of being wrong!
    As hinted at, 'new' never returns NULL any more; it just throws.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exceptions "try, catch, and throw Statements" ???
    By Loic in forum C++ Programming
    Replies: 2
    Last Post: 08-12-2008, 09:22 PM
  2. Throw Catch for External Program
    By dav_mt in forum C++ Programming
    Replies: 6
    Last Post: 04-19-2008, 09:52 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Try...catch...throw or ifs?
    By Kylecito in forum C++ Programming
    Replies: 9
    Last Post: 03-02-2006, 10:41 PM
  5. try catch throw
    By ygfperson in forum C++ Programming
    Replies: 2
    Last Post: 07-27-2003, 02:15 AM