catch()

This is a discussion on catch() within the C++ Programming forums, part of the General Programming Boards category; I am confused about the bolded part. Why do we have to throw Overflow(), Underflow() and Bad_size() and not just ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272

    catch()

    I am confused about the bolded part.

    Why do we have to throw Overflow(), Underflow() and Bad_size() and not just throw Overflow, Underflow... without the parantheses - why do we have to throw a "function" and not the type of the object itself?

    And same goes for structures, not just classes.

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    class Stack {
          char *v;
          int top;
          int max_size;
          public:
                 class Overflow { };
                 class Underflow { };
                 class Bad_Size { };
                 struct ov {
                 };
                 
                 Stack(int s);
                 ~Stack();
                 
                 void push(char c);
                 char pop();
    };
    
    Stack :: Stack(int s)
    {
          top = 0;
          if(s > 10000) 
             throw Bad_Size();
          max_size = s;
          v = new char[s];
    }
    
    Stack :: ~Stack()
    {
          delete [] v;
    }
    
    void Stack :: push(char c)
    {
         if(top == max_size - 1)
            throw Overflow();
         v[top++] = c;
    }
    
    char Stack :: pop()
    {
         if(top == 0)
            throw Underflow();
         return v[--top];
    }
    
    int main(int argc, char *argv[])
    {
          Stack :: Bad_Size();
          try {
              Stack sp(10000);
              int i;
              for(i = 0; i <= 5; i++)
                 sp.push('c');
          }
          catch(Stack :: Underflow) {
              cout << "underflow!" << '\n';
          }
          catch(Stack :: Overflow) {
              cout << "overflow!" << '\n';
          }
          catch(Stack :: Bad_Size) {
              cout << "bad size!" << '\n';
          }
          Stack :: Bad_Size();
          getchar();
          return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,620
    The throw/catch mechanism works by copying an object of some type from the catch point to the throw point (and by unwinding the stack in the process). A type is not an object.

    Overflow is a (class) type. Overflow() creates an object of that type (created by invoking the default constructor).

    A struct and a class in C++ are the same thing (or, more precisely, a struct is a class with all members public by default rather than private).

    If you want to avoid something that (superficially) looks like a function call, replace;
    Code:
    if(top == 0)
      throw Underflow();
    with
    Code:
    if(top == 0)
    {
         Underflow my_exception;
         throw my_exception;
    }
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    (created by invoking the default constructor).
    So you mean that even if the class doesnt' have any constructors specified in it, it has one by default - and that is <class name>()?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    Yes. In fact, if you do declare other constructors, the default constructor would not be provided automatically.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. catch hotkeys while not active keyboard focus
    By zeroth in forum Linux Programming
    Replies: 2
    Last Post: 12-15-2009, 05:25 PM
  2. Throw catch question
    By TriKri in forum C++ Programming
    Replies: 2
    Last Post: 10-25-2009, 04:32 AM
  3. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05:30 PM
  4. What can be done with catch(...)?
    By 6tr6tr in forum C++ Programming
    Replies: 6
    Last Post: 04-17-2008, 11:27 AM
  5. Need advice: catch exceptions or call methods to check bits?
    By registering in forum C++ Programming
    Replies: 1
    Last Post: 10-03-2003, 02:49 PM

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