explain code

This is a discussion on explain code within the C++ Programming forums, part of the General Programming Boards category; Code: // Ex6_04.cpp Using exception handling #include <iostream> using std::cout; using std::endl; int main(void) { int counts[] = {34, 54, ...

  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
    21,712
    You might want to read up on exceptions.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    4,246
    *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
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    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, 03: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, 12: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-15-2002, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21