Thread: explain code

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    164

    explain code

    Code:
    // Ex6_04.cpp  Using exception handling
    #include <iostream>
    using std::cout;
    using std::endl;
    
    int main(void)
    {
      int counts[] = {34, 54, 0, 27, 0, 10, 0};
      int time = 60;                       // One hour in minutes
    
      for(int i = 0 ; i < sizeof counts/sizeof counts[0] ; i++)
        try
        {
          cout << endl
               << "Hour " << i+1;
    
          if(counts[i] == 0)
            throw "Zero count - calculation not possible.";
    
          cout << " minutes per item: "
               << static_cast<double>(time)/counts[i];
        }
        catch(const char aMessage[])
        {
          cout << endl
               << aMessage
               << endl;
        }
      return 0;
    }
    Can any one explain the above code

    I cannot understand the catch part
    Is catch a function? from where the const char aMessage[] came from? what does it containt.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You might want to read up on exceptions.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    catch is the receiving part of a throw [both catch and throw are C++ keywords] - that is part of exception handling. You probably need to re-read the chapter of the book that deals with exception handling if you don't understand how that works [it's both simple (as a concept) and complex (as in there are many different things you can do, that does things a bit differently)].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    my book doesn't explains that part,

    I might search in google

    thx anyways

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by manzoor View Post
    my book doesn't explains that part,

    I might search in google

    thx anyways
    So where does the exercise come from, if it's not part of your book?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    *sigh*

    A perfect example of how not to use exception coupled with a bad catch and obtuse source structure. ;_;

    Soma

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    I'm saying that my book doesn't explains the catch part of the code, it explains everything else

    the code is from the book btw

    So may I know that how the catch can have arguments ?
    Last edited by manzoor; 09-15-2008 at 04:39 AM.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    catch (type_of_exception var_name) {
        access and use var_name
    }
    I agree with phantomotap that it is a rather bad example of using exceptions, though. Exceptions are more like smart error codes for cases where you don't know what do about an error condition within the function (other than that you can't continue and the calling function - or the functions above that - should do something). In this example you do: print a message and carry on with the next item, so you might just print the message where you throw it instead.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Looks like it's from Beginning Visual C++ 2005, from the chapter on Program Structure, which includes a section on...

    wait for it...

    Exceptions!


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing Code
    By ILoveVectors in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2005, 12:27 AM
  2. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  3. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 01:33 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM