Using exception classes. (urgent)

This is a discussion on Using exception classes. (urgent) within the C++ Programming forums, part of the General Programming Boards category; Ok so i have 2 programs due in 2 hours I have everything working, or so i believe except throwing ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    68

    Using exception classes. (urgent)

    Ok so i have 2 programs due in 2 hours

    I have everything working, or so i believe except throwing exceptions. I have no idea how to go about this and can not find anything useful. My teacher is long gone to bed lol so im in trouble

    Anyone who could enlighten me on this would be a savior thank you. I understand what they need to do. Throwing and catching per-say. im just not sure on the actual code.

    Here is the main program he provided. I realize this is long but I'm just flabbergausted on what to do

    Code:
    /* To compile this program, do the following
    aCC olab10BSTreeMain.cpp BSTree.cpp
    To execute with test data file
    a.out < /nfshome/csal/public_html/spring.10/2170/olab10sp10.dat
    
    uses exceptions
    As always let me know if there are syntax errors in program
    */
    
    #include <iostream>
    #include "BSTree.h"
    using namespace std;
    
    int main()
    {
    
       BSTree MyTree;			// A binary search tree used in examples
    
       // See what happens when an empty tree is printed
       cout << "An empty tree:" << MyTree << "*" << endl;
       cout << "Count: " << MyTree.Count() << endl;
       try
       {
          cout << "First String: " << MyTree.First() << endl;
       }
       catch (NonExistent myexception)
       {
         cerr << "There are data itemd in the tree" << endl; 
       }
    
       try
       {
         cout << "Last String: " << MyTree.Last() << endl;
       }
       catch (NonExistent myexception)
       {
         cerr << "There are data itemd in the tree" << endl; 
       }
      
       cin >> MyTree;		// read's into
       cout << "Tree after first read: " << MyTree << endl;;
       cout << "Count: " << MyTree.Count() << endl;
       try
       {
          cout << "First String: " << MyTree.First() << endl;
       }
       catch (NonExistent myexception)
       {
         cerr << "There are data itemd in the tree" << endl; 
       }
    
       try
       {
         cout << "Last String: " << MyTree.Last() << endl;
       }
       catch (NonExistent myexception)
       {
         cerr << "There are data itemd in the tree" << endl; 
       }
    
       MyTree.Insert("into");  	// insert new root
       cout << "Tree after insert: " << MyTree << endl;;
    
       cin >> MyTree;		// read's insert
       cout << "Tree after second read: " << MyTree << endl; // This does inorder traversal
       cout << "Count: " << MyTree.Count() << endl;
       try
       {
          cout << "First String: " << MyTree.First() << endl;
       }
       catch (NonExistent myexception)
       {
         cerr << "There are data itemd in the tree" << endl; 
       }
    
       try
       {
         cout << "Last String: " << MyTree.Last() << endl;
       }
       catch (NonExistent myexception)
       {
         cerr << "There are data itemd in the tree" << endl; 
       }
       cin >> MyTree;		// read's into
       cin >> MyTree;		// read's link
       cin >> MyTree;		// read's list
       cout << "Tree after read: " << MyTree << endl; // This does inorder traversal
       cout << "Count: " << MyTree.Count() << endl;
       try
       {
         cout << "First String: " << MyTree.First() << endl;
       }
       catch (NonExistent myexception)
       {
         cerr << "There are data itemd in the tree" << endl; 
       }
       try
       {
         cout << "Last String: " << MyTree.Last() << endl;
       }
       catch (NonExistent myexception)
       {
         cerr << "There are data itemd in the tree" << endl; 
       }
       cin >> MyTree;		// read one string into the tree
       cout << "Tree after read: " << MyTree << endl; // This does inorder traversal
       cout << "Count: " << MyTree.Count() << endl;
       try
       {
         cout << "First String: " << MyTree.First() << endl;
       }
       catch (NonExistent myexception)
       {
         cerr << "There are data itemd in the tree" << endl; 
       }
       try
       {
         cout << "Last String: " << MyTree.Last() << endl;
       }
       catch (NonExistent myexception)
       {
         cerr << "There are data itemd in the tree" << endl; 
       }
       cin >> MyTree;		// read's next
       cin >> MyTree;		// read's last
       cout << "Tree after read: " << MyTree << endl; // This does inorder traversal
       cout << "Count: " << MyTree.Count() << endl;
       try
       {
         cout << "First String: " << MyTree.First() << endl;
       }
       catch (NonExistent myexception)
       {
         cerr << "There are data itemd in the tree" << endl; 
       }
       try
       {
         cout << "Last String: " << MyTree.Last() << endl;
       }
       catch (NonExistent myexception)
       {
         cerr << "There are data itemd in the tree" << endl; 
       }
       cout << "Inorder traversal: " << endl;
       MyTree.InOrderTraversal();
       cout << endl;
       cout << "Preorder traversal: " << endl;
       MyTree.PreOrderTraversal();
       cout << endl;
       cout << "Postorder traversal: " << endl;
       MyTree.PostOrderTraversal();
       cout << endl;
    
       cin >> MyTree;		// read's link
       cin >> MyTree;		// read's insurance
       cout << "Tree after read: " << MyTree << endl; // This does inorder traversal
       cout << "Count: " << MyTree.Count() << endl;
       try
       {
         cout << "First String: " << MyTree.First() << endl;
       }
       catch (NonExistent myexception)
       {
         cerr << "There are data itemd in the tree" << endl; 
       }
       try
       {
         cout << "Last String: " << MyTree.Last() << endl;
       }
       catch (NonExistent myexception)
       {
         cerr << "There are data itemd in the tree" << endl; 
       }
       cout << "Inorder traversal: " << endl;
       MyTree.InOrderTraversal();
       cout << endl;
       cout << "Preorder traversal: " << endl;
       MyTree.PreOrderTraversal();
       cout << endl;
       cout << "Postorder traversal: " << endl;
       MyTree.PostOrderTraversal();
       cout << endl;
    
       cin >> MyTree;		// read's help
       cin >> MyTree;		// read's insert
       cout << "Tree after read: " << MyTree << endl; // This does inorder traversal
       cout << "Count: " << MyTree.Count() << endl;
       try
       {
         cout << "First String: " << MyTree.First() << endl;
       }
       catch (NonExistent myexception)
       {
         cerr << "There are data itemd in the tree" << endl; 
       }
       try
       {
         cout << "Last String: " << MyTree.Last() << endl;
       }
       catch (NonExistent myexception)
       {
         cerr << "There are data itemd in the tree" << endl; 
       }
       cout << "Inorder traversal: " << endl;
       MyTree.InOrderTraversal();
       cout << endl;
       cout << "Preorder traversal: " << endl;
       MyTree.PreOrderTraversal();
       cout << endl;
       cout << "Postorder traversal: " << endl;
       MyTree.PostOrderTraversal();
       cout << endl;
    
       cout << "Count: " << MyTree.Count() << endl;
       try
       {
         cout << "First String: " << MyTree.First() << endl;
       }
       catch (NonExistent myexception)
       {
         cerr << "There are data itemd in the tree" << endl; 
       }
       try
       {
         cout << "Last String: " << MyTree.Last() << endl;
       }
       catch (NonExistent myexception)
       {
         cerr << "There are data itemd in the tree" << endl; 
       }
       cout << "Inorder traversal: " << endl;
       MyTree.InOrderTraversal();
       cout << endl;
       cout << "Preorder traversal: " << endl;
       MyTree.PreOrderTraversal();
       cout << endl;
       cout << "Postorder traversal: " << endl;
       MyTree.PostOrderTraversal();
       cout << endl;
    
       cin >> MyTree;		// read's justice
       cout << "Tree after read: " << MyTree << endl; // This does inorder traversal
       cout << "Count: " << MyTree.Count() << endl;
       try
       {
         cout << "First String: " << MyTree.First() << endl;
       }
       catch (NonExistent myexception)
       {
         cerr << "There are data itemd in the tree" << endl; 
       }
       try
       {
         cout << "Last String: " << MyTree.Last() << endl;
       }
       catch (NonExistent myexception)
       {
         cerr << "There are data itemd in the tree" << endl; 
       }
       cout << "Inorder traversal: " << endl;
       MyTree.InOrderTraversal();
       cout << endl;
       cout << "Preorder traversal: " << endl;
       MyTree.PreOrderTraversal();
       cout << endl;
       cout << "Postorder traversal: " << endl;
       MyTree.PostOrderTraversal();
       cout << endl;
    
       cin >> MyTree;		// read's just
       cout << "Tree after read: " << MyTree << endl; // This does inorder traversal
       cout << "Count: " << MyTree.Count() << endl;
       try
       {
         cout << "First String: " << MyTree.First() << endl;
       }
       catch (NonExistent myexception)
       {
         cerr << "There are data itemd in the tree" << endl; 
       }
       try
       {
         cout << "Last String: " << MyTree.Last() << endl;
       }
       catch (NonExistent myexception)
       {
         cerr << "There are data itemd in the tree" << endl; 
       }
       cout << "Inorder traversal: " << endl;
       MyTree.InOrderTraversal();
       cout << endl;
       cout << "Preorder traversal: " << endl;
       MyTree.PreOrderTraversal();
       cout << endl;
       cout << "Postorder traversal: " << endl;
       MyTree.PostOrderTraversal();
       cout << endl;
    
       // test copy constructor
       BSTree CopyTree(MyTree);
       cout << "Copy Tree: " << CopyTree << endl;;
    
       cout << "end of main"<<endl;
       return 0;
    }
    and here is the code im working on

    Code:
    #include <string>
    
    using namespace std;
    
    // THIS IS WHERE IM HAVING TROUBLE
    class myexception
    {
    public:
    	void NonExistent();
    };
    
    struct node 
    {
    	string data;
    	node *left;
    	node *right;
    };
    
    
    class BSTree
    {
    public:
    	//Define deconstructor
    	~BSTree();
    	void  DestroyTree();
    
    	void Insert(string);
    	void Delete(string);
    	void InOrderTraversal();
    	void PreOrderTraversal();
    	void PostOrderTraversal();
    	int Count();
    	string First();
    	string Last();
    
    private:
    	void DestroyTree(node *current);
    	void InOrderTraversal(node *current);
    	void PreOrderTraversal(node *current);
    	void PostOrderTraversal(node *current);
    	int Count(node *current);
    	string First(node *current);
    	string Last(node *current);
    
    	node *root;
    };
    
    void myexception::NonExistent()
    {
    	cerr << "Error";
    }
    
    
    BSTree::~BSTree()
    {
    	DestroyTree();
    }
    
    void BSTree::DestroyTree()
    {
    	DestroyTree(root);
    }
    
    void BSTree::DestroyTree(node *current)
    {
      if (current != NULL)
      {
        DestroyTree(current->left);
    	DestroyTree(current->right);
        delete current;
      }
    }
    
    void BSTree::Insert(string data)
    {
    
    	node *current = new node;
    	node *parent = new node;
    
    	current = root;
    	parent = NULL;
    
    	while (current != NULL)
    	{
    		parent = current;
    
    		if (data < current->data)
    		{
    			current = current->left;
    		}
    		else
    		{
    			current = current->right;
    		}
    		}
    
    		if (parent == NULL)
    		{
    			root->data = data;
    		}
    		else if (data < parent->data)
    		{
    			parent->left->data = data;
    		}
    		else
    		{
    			parent->right->data = data;
    		}
    
    }
    
    //In order public and private functions
    void BSTree::InOrderTraversal()
    {
    	if (root == NULL)
    	{
    		return;
    	}
    	else
    	{
    		InOrderTraversal(root);
    	}
    }
    
    void InOrderTraversal(node *current)
    {
    	if (current == NULL)
    	{
    		return;
    	}
    	else
    	{
    		InOrderTraversal(current->left);
    		cout << current->data;
    		InOrderTraversal(current->right);
    	}
    }
    
    //Post order public and private functions
    void BSTree::PostOrderTraversal()
    {
    	if (root == NULL)
    	{
    		return;
    	}
    	else
    	{
    		PostOrderTraversal(root);
    	}
    }
    
    void PostOrderTraversal(node *current)
    {
    	if (current == NULL)
    	{
    		return;
    	}
    	else
    	{
    		PostOrderTraversal(current->left);
    		PostOrderTraversal(current->right);
    		cout << current->data;
    	}
    }
    
    //Pre order public and private functions
    void BSTree::PreOrderTraversal()
    {
    	if (root == NULL)
    	{
    		return;
    	}
    	else
    	{
    		PreOrderTraversal(root);
    	}
    }
    
    void PreOrderTraversal(node *current)
    {
    	if (current == NULL)
    	{
    		return;
    	}
    	else
    	{
    		cout << current->data;
    		PreOrderTraversal(current->left);
    		PreOrderTraversal(current->right);
    	}
    }
    
    //Count functions
    int BSTree::Count()
    {
    	if (root == NULL)
    	{
    		return 0;
    	}
    	else
    	{
    		Count(root);
    	}
    }
    
    int Count(node *current)
    {
    	if (current == NULL)
    	{
    		return 0;
    	}
    	else
    	{
    	int count = 1;
    
    	count += Count(current->left);
    
    	count += Count(current->right);
    
    	return count;
    	}
    }
    
    //Print first functions
    string BSTree::First()
    {
    
    	if (root == NULL)
    	{
    		return 0;
    		//throw NonExistent();
    	}
    	else
    	{
    		First(root);
    	}
    }
    
    //Print second functions
    string First(node *current)
    {
    	if (current->left == NULL)
    	{
    		cout << current->left->data;
    	}
    	else
    	{
    		First(current->left);
    	}
    }
    
    string BSTree::Last()
    {
    
    	if (root = NULL)
    	{
    		return 0;
    		//throw NonExistent();
    	}
    	else
    	{
    		Last(root);
    	}
    }
    
    string Last(node *current)
    {
    	if (current->right == NULL)
    	{
    		cout << current->right->data;
    	}
    	else
    	{
    		First(current->right);
    	}
    } 
    
    void BSTree::Delete(string data)
    {
    
    	bool found = false;
    
        if (root == NULL)
    	{
    		//exception
    	}
    
        node *current = new node;
        node *parent = new node;
        current = root;
    
        while(current)
        {
            if(current->data == data)
            {
                found = true;
                break;
            }
            else
            {
                parent = current;
    
                if(current->data ==data)
    			{
                    current = current->right;
    			}
    			else
    			{
                    current = current->left;
    			}
            }
        }
        if(!found)
        {
            //exception
        }
    
    
        // 3 cases :
        // 1. We're removing a leaf node
        // 2. We're removing a node with a single child
        // 3. we're removing a node with 2 children
    
        // Node with single child
        if((current->left == NULL && current->right != NULL) || (current->left != NULL && current->right == NULL))
        {
            if(current->left == NULL && current->right != NULL)
            {
                if(parent->left == current)
                {
                    parent->left = current->right;
                    delete current;
                }
                else
                {
                    parent->right = current->right;
                    delete current;
                }
            }
            else // left child present, no right child
            {
                if(parent->left == current)
                {
                    parent->left = current->left;
                    delete current;
                }
                else
                {
                    parent->right = current->left;
                    delete current;
                }
            }
            return;
        }
    
        //We're looking at a leaf node
        if( current->left == NULL && current->right == NULL)
        {
            if(parent->left == current)
    		{
                parent->left = NULL;
    		}
    		else
    		{
                parent->right = NULL;
    		}
    
    		delete current;
            return;
        }
    
    
        //Node with 2 children
        // replace node with smallest value in right subtree
        if (current->left != NULL && current->right != NULL)
        {
            node *chkr = new node;
            chkr = current->right;
            if((chkr->left == NULL) && (chkr->right == NULL))
            {
                current = chkr;
                delete chkr;
                current->right = NULL;
            }
            else // right child has children
            {
                //if the node's right child has a left child
                // Move all the way down left to locate smallest element
    
                if((current->right)->left != NULL)
                {
                    node *lcurr = new node;
                    node *lcurrp = new node;
                    lcurrp = current->right;
                    lcurr = (current->right)->left;
                    while(lcurr->left != NULL)
                    {
                        lcurrp = lcurr;
                        lcurr = lcurr->left;
                    }
                    current->data = lcurr->data;
                    delete lcurr;
                    lcurrp->left = NULL;
                }
                else
                {
                    node *tmp = new node;
                    tmp = current->right;
                    current->data = tmp->data;
                    current->right = tmp->right;
                    delete tmp;
                }
    
            }
            return;
        }
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    Quote Originally Posted by tabstop View Post
    Yes i have and i guess im just not following it.

    So since he uses this line in the .cpp file

    catch (NonExistent myexception)

    do i need something in the class called NonExistent?

    what would the myexception be? something like myexception.NonExistent ??

    like in this line

    catch (DivideByZero divZero)

    I see where DivideByZero comes from. buttt where does divZero come from?

    im sorry im just super lost. everything i read doesnt make a lot of sense

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Nonexistent is the type, myexception is the name of the variable of that type.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    Quote Originally Posted by tabstop View Post
    Nonexistent is the type, myexception is the name of the variable of that type.
    gah man i understand what you are saying but im not grasping it AGRH! lol

    can you type up an example...

    if i did like
    NonExistent.myexception();

    would that work?

    so my class

    Code:
    class myexception
    {
         void NonExistent();
    };
    is this right ? not enough? do I need like a throw () in there somewhere.

    I really appreciate any help man.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Compare and contrast (except it's mostly compare):
    Code:
    int i;
    Code:
    Nonexistent myexception;
    Do you ever go around talking about int.this and int.that? Of course not. You can only talk about members of objects, not members of classes.

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    ARGGHH! lol i understand what you are saying. Im sorry but I just can't think how to implement it.

    So is my class part right or am i really missing something?

    and when i call the exception in the function.

    would I do something like

    NonExistent... < arghh idk >

    I dont want you to just hand me an answer but can you give a example related more to my code?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by bleuz View Post
    ARGGHH! lol i understand what you are saying. Im sorry but I just can't think how to implement it.

    So is my class part right or am i really missing something?

    and when i call the exception in the function.

    would I do something like

    NonExistent... < arghh idk >

    I dont want you to just hand me an answer but can you give a example related more to my code?
    Out of your current exception class, you can keep the word "class". Everything else is not even remotely related to what needs to be there -- the name of the class is Nonexistent, so call it that. There is no data inside it, so don't put anything in there. When you need to throw something, create an object of the Nonexistent class and throw it.

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    Quote Originally Posted by tabstop View Post
    Out of your current exception class, you can keep the word "class". Everything else is not even remotely related to what needs to be there -- the name of the class is Nonexistent, so call it that. There is no data inside it, so don't put anything in there. When you need to throw something, create an object of the Nonexistent class and throw it.
    wow idk why but that sorta helped
    so

    Code:
    class NonExistent
    {
    
    };
    
    NonExistent myexception()
    {
    throw 1;
    }
    Is this along the right lines?

    or do i need something else

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by bleuz View Post
    wow idk why but that sorta helped
    so

    Code:
    class NonExistent
    {
    
    };
    
    NonExistent myexception()
    {
    throw 1;
    }
    Is this along the right lines?

    or do i need something else
    You're up to 50%. The first four lines make sense. The last four lines do not. Where in the name of all that is good do you get the idea that a function is in any way, shape, or form involved in this process?

  11. #11
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    Oooo. or do i need something like


    Code:
    head.h
    
    class NonExistent
    {
    };
    ...
    ...
    ...
    main.cpp
    
    if ( x = 1 )
         throw 2;
    catch (NonExistent 2)
         cout << "error";

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So you keep saying you've read something about exceptions. At this point I am forced to say I do not believe you.

    You need to throw an object of type Nonexistent, when you get to all those points in somebody's-but-let's-call-it-your code that say //throw exception. That is all there is for you to do. It is not your job to do any catching. Look at the main code! Do you not see all those try/catch blocks?

  13. #13
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    Quote Originally Posted by tabstop View Post
    So you keep saying you've read something about exceptions. At this point I am forced to say I do not believe you.

    You need to throw an object of type Nonexistent, when you get to all those points in somebody's-but-let's-call-it-your code that say //throw exception. That is all there is for you to do. It is not your job to do any catching. Look at the main code! Do you not see all those try/catch blocks?
    Everything I read was much more complex and I over-analyzed it. I knew I didn't need to catch it. I guess I was expecting to have to do more for it from what I read.

    I appreciate all of your assistance and hopefully in the future it will all make sense.

  14. #14
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    You need to create a type, that you want to throw and later catch. In the catch block, you declare a variable of that type so you can access the element, that has been thrown. For example:

    Code:
    #include <iostream>
    #include <string>
    
    class SomethingFailedException
    {
    private:
    	std::string desc;
    
    public:
    	SomethingFailedException( const std::string& description )
    	{
    		desc = description;
    	}
    
    	std::string GetDescription() const
    	{
    		return desc;
    	}
    };
    
    void FunctionThatFailsAndThrows()
    {
    	throw SomethingFailedException("Oh no, the function failed!");
    }
    
    int main()
    {
    	try
    	{
    		FunctionThatFailsAndThrows();
    	}
    	catch( SomethingFailedException ex )
    	{
    		std::cout << "Something failed: " << ex.GetDescription() << std::endl;
    	}
    
    	return 0;
    }
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  15. #15
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You don't need anything in your exception class unless you want the exception to be able to contain a message or some other data that might be useful in explaining why the exception was thrown.
    I usually just derive my own exceptions from std::exception and leave it at that.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exception handling in a large project
    By EVOEx in forum C++ Programming
    Replies: 7
    Last Post: 01-25-2009, 07:33 AM
  2. exception handling
    By coletek in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2009, 05:28 PM
  3. unhandled exception (URGENT)
    By Lollipop in forum C++ Programming
    Replies: 8
    Last Post: 08-06-2003, 11:02 AM
  4. exception handling classes in IRIX....?
    By BrianK in forum C++ Programming
    Replies: 2
    Last Post: 01-28-2003, 03:02 PM
  5. exception classes
    By elad in forum C++ Programming
    Replies: 5
    Last Post: 11-01-2002, 12:39 PM

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