Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Exception in C++

  1. May 23, 2008 #1

    Defennder

    User Avatar
    Homework Helper

    Code (Text):
    void List::insert(int index, const ListItemType& newItem)
    [COLOR="Red"]throw(ListIndexOutOfRangeException, ListException)[/COLOR]
    {
        int newLength = getLength() + 1;
        if ( (index < 1) || (index > newLength) )
            [COLOR="Blue"]throw ListIndexOutOfRangeException("Bad index in insert");[/COLOR]
        else
        { // try to create new node and place newItem in it
            ListNode *newPtr = new ListNode;
            size = newLength;
            newPtr->item = newItem;

            // attach new node to list
            if (index == 1)
            { // insert new node at beginning of list
                newPtr->next = head;
                head = newPtr;
            }
            else
            { ListNode *prev = find(index-1);
                // insert new node after node
                // to which prev points
                newPtr->next = prev->next;
                prev->next = newPtr;
            } // end if
        } // end insert
    The above is from my notes on linked lists. Specifically it is the insert member function of linked list. I see that there are 2 possible things which may be thrown, but in the member function implementation, only one is set up to be thrown in case of out of range error. What of the other?
     
  2. jcsd
  3. May 23, 2008 #2
    Maybe it could be thrown by one of the functions called in insert?

    Sidenote: from what I've read, exception specifications are really not worth your time (though that might be debatable). Here's a page that outlines some reasons why: http://www.gotw.ca/publications/mill22.htm
     
  4. May 23, 2008 #3

    Defennder

    User Avatar
    Homework Helper

    Well looking at the 'insert' function, I see that only getlength() and find(index-1) have been called. But it appears that neither of them has been set up to throw anything at all:

    Code (Text):
    List::ListNode *List::find(int index) const
    {
    if ( (index < 1) || (index > getLength()) )
    return NULL;
    else // count from the beginning of the list.
    {
    ListNode *cur = head;
    for (int skip = 1; skip < index; ++skip)
    cur = cur->next;
    return cur;
    } // end if
    } // end find
    Code (Text):
    int List::getLength() const
    {
    return size;
    } // end getLength
     
  5. May 24, 2008 #4

    Defennder

    User Avatar
    Homework Helper

    One more question, I thought the only time we state in the function prototype declaration for eg. "throw (ExceptionOne, ExceptionTwo)" is when both these are set up to be thrown in that member function itself and not in other functions which happen to throw them, but which are called by this member function?
     
  6. May 24, 2008 #5
    Well I'm not entirely sure. If you don't catch the exceptions from functions you're invoking in the function, then I think it effectively gets rethrown. And anyways, I tried this on GCC (though not the latest version since I haven't been programming much recently):
    Code (Text):

    #include <iostream>


    class Foo
    {
    public:
      class FooException{};
      class FooBarException{};

      void Baz() { throw FooException(); }
      void Bar() throw (FooBarException);


    };





    void My_Unexpected()
    {
      std::cerr<<"Unexpected called!";
    }

    int main()
    {
       std::set_unexpected(My_Unexpected);
       Foo f;
       try
       {
       f.Bar();
       }
       catch (Foo::FooException& e)
       {
           std::cerr<<"FooException";
       }
       catch(...)
       {
         std::cerr<<"...";
       }
    }
     
    The output was "Unexpected called!" Which makes sense since the compiler will essentially do something like:
    Code (Text):

    void Foo::Bar()
    throw (FooBarException)
    {
      try
      {
        Baz();
      }
      catch(FooBarException)
      {
        throw;
      }
      catch(...)
      {
         unexpected();
      }
    }
     
    As for your original question, I suppose specifying an exception that isn't thrown isn't necessarily a bad thing. It tells anyone who uses your class what exceptions they need to handle, so in the future if you change your class you can use the "ListException" for something, and if you've already planned for it to throw that exception, it doesn't break existing code that uses insert.

    That said, I still don't think exception specification's are a good thing in general.
     
  7. May 25, 2008 #6

    Defennder

    User Avatar
    Homework Helper

    What do you mean by "rethrown"? Meaning to say that if you were to write a throw(exception) in the member function implementation, but not set up something in main() to catch it, then what happens?

    And I couldn't get your code to compile at all. What is "catch(...)" supposed to contain?

    EDIT:I don't like exception handling as well, but my course uses it extensively. So it's best if I familiarise myself with it properly, even if I choose not to use it in practice.
     
    Last edited: May 25, 2008
  8. May 26, 2008 #7
    The second example in my post wasn't meant to be compiled, though it looks compileable to me. catch(...) means catch any exception that isn't already handled (so you get no specific information, only that some unkown exception was thrown)

    Just to be clear, I think exceptions themselves are useful, but writing exception specifications (the list of what exceptions a given function will throw) isn't.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?