Thread: exception handling

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    7

    exception handling

    just one of my functions isn't throwing exceptions for some reason. this one doesn't work:
    Code:
    void list::insert(const plane& p) throw(listException)
    {
    	int i = 0;
    
    	if (size >= MAX_LIST)
    		throw listException("List is full"); // <--doesn't work
    	
    	while (i != size && planeList[i] < p)
    		i++;
    
    	if (i != size && planeList[i] == p)
    		throw listException("Duplicate plane - not inserted."); // <-- doesn't work
    
    	//right shift
    	for (int pos = size; pos > i; pos--)
    	{
    		planeList[pos] = planeList[pos - 1];
    
    		if (pos < 1 || pos > size)
    			throw listException("Bad index on insert"); // <-- doesn't work
    	}
    
    	planeList[i] = p;
    	size++;
    }
    but it works over here in this function:
    Code:
    void list::remove(const plane& p) throw(listException)
    {
    	int i = 0;
    
    	if (size == 0) //list is empty?
    		throw listException("List is empty"); //<-- works
    	
    	while (i != size && planeList[i] < p)
    		i++;
    
    	//left shift
    	for (int pos = 1; pos < size + 1; pos++)
    	{
    		if (pos >= i) //dont move any to the left of i
    			planeList[pos] = planeList[pos + 1];
    	}
    	
    	//display no delete if input is invalid
    	if (planeList[i] == p)
    		cout << endl << "Plane successfully deleted." << endl << endl;
    	else
    		throw listException("List is empty"); //<-- works
    
    	size--;
    }
    i dont get it.....here's the listException class
    Code:
    //Filename: listException.h
    
    #include <cstring>
    #pragma once
    
    using namespace std;
    
    class listException
    {
    public:
    	listException(const char msg[] = "")
    	{
    		strcpy(message, msg);
    	}
    
    	const char* what()
    	{
    		return message;
    	}
    private:
    	char message[50];
    };
    here's the call
    Code:
    //....
    case 2:
    				//delete a plane
    				p = getModelAndNumber();
    				try
    				{
    					l.remove(p);
    				}
    				catch(listException ex)
    				{
    					cout << endl << "Exception Occurred: " << ex.what() << endl << endl;
    				}
    				break;
    //....

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Where do you call insert? What is the value of size and MAX_LIST when you expect the exception to be thrown. All you did was show the code where the one that's working is called.

    BTW, you normally want to catch exceptions by reference.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I'd also recommend using std::string rather than a char[50] array. Right now, you can easily overflow your buffer if you create an exception and pass a string longer than 49 characters.

    Also, don't use exception specifications. They have no benefits - only drawbacks.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In exceptions it is sometimes wise to use static arrays over std::string because if std::string attempts to allocate memory and fails, then it will throw its own exception, and if that happens during the processing of your exception it means the entire program aborts with no cleanup or destructor calls or anything. That's why people often use character arrays for exceptions and just document that the message must be less than a certain size.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by Daved View Post
    In exceptions it is sometimes wise to use static arrays over std::string because if std::string attempts to allocate memory and fails, then it will throw its own exception, and if that happens during the processing of your exception it means the entire program aborts with no cleanup or destructor calls or anything. That's why people often use character arrays for exceptions and just document that the message must be less than a certain size.
    Couldn't you get the same behavior by using reserve() with the std::string to set the capacity to the desired value beforehand?

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No. Exceptions must be copyable, and the copy constructor of std::string allocates memory.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    7
    woops i meant to put in the insert call, but i found the problem anyway. thanks

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Daved View Post
    In exceptions it is sometimes wise to use static arrays over std::string because if std::string attempts to allocate memory and fails, then it will throw its own exception, and if that happens during the processing of your exception it means the entire program aborts with no cleanup or destructor calls or anything. That's why people often use character arrays for exceptions and just document that the message must be less than a certain size.
    Of course, you can still allow a std::string to be used in the constructor, you just have to convert to a C string in order to store it:

    Code:
    struct exception
    {
        exception(const std::string &what_p)
        {
            strncpy(desc, what_p.c_str(), sizeof(desc));
            desc[sizeof(desc)-1] = '\0';
        }
    
        const char *what() const { return desc; }
    
    private:
        char desc[128];
    };

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. signal handling and exception handling
    By lehe in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2009, 10:01 PM
  2. Exception handling in a large project
    By EVOEx in forum C++ Programming
    Replies: 7
    Last Post: 01-25-2009, 07:33 AM
  3. exception handling
    By coletek in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2009, 05:28 PM
  4. is such exception handling approach good?
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 12-27-2007, 08:54 AM
  5. Signal and exception handling
    By nts in forum C++ Programming
    Replies: 23
    Last Post: 11-15-2007, 02:36 PM