Thread: how to throw an Overflow() exception

  1. #1
    CIS and business major
    Join Date
    Aug 2002
    Posts
    287

    how to throw an Overflow() exception

    I'm trying to throw an overflow exception:

    Code:
    void Stack::push(char c)
    {
             if(top == max_size) throw Overflow();
    }

    but I can't find the right header in visual C++ express 2010. Is it #include <stack> ? I'm trying this and it still won't compile.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Although it is more for dynamic memory allocation, I think std::bad_alloc may be the most appropriate, i.e., #include <new> and write:
    Code:
    void Stack::push(char c)
    {
        if (top == max_size)
            throw std::bad_alloc();
    }
    Quote Originally Posted by Terrance
    Is it #include <stack> ?
    If you are going to use std::stack<char> then you don't need to write your own stack in the first place.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. throw exception in Try Block and catch
    By mirshany in forum C++ Programming
    Replies: 3
    Last Post: 06-10-2011, 03:50 AM
  2. Exception specifications: throw() useful?
    By Memloop in forum C++ Programming
    Replies: 7
    Last Post: 11-25-2009, 02:24 AM
  3. STL sort throw exception?
    By George2 in forum C++ Programming
    Replies: 11
    Last Post: 01-10-2008, 06:34 AM
  4. re-throw an exception
    By filler_bunny in forum C++ Programming
    Replies: 7
    Last Post: 10-15-2003, 06:06 AM
  5. std::bad_exception donīt throw exception
    By ripper079 in forum C++ Programming
    Replies: 2
    Last Post: 03-20-2003, 07:47 PM

Tags for this Thread