-1

I am having a very tough time understanding exception handling after watching online tutorials and reading up on it. I am trying to pass test driven development, and I can't. What I have come up with so far is this. I am supposed to use this struct

struct ArrayException
{
    ArrayException(string newMessage = "error") :message(newMessage)
    {
    }

    string message;
};

The first try.

int sum(int* theArray, unsigned int arraySize)
{
    try
    {

        if (theArray = NULL)
        {
            throw ArrayException("NULL ARRAY REFERENCE");
        }
    }
    catch (int* param)
    {
        cout << "you can't have " << param << " as an array size";
    }
    int sum = 0;
    for (int i = 1; i < arraySize; i++)
    {
        sum += theArray[i];
    }
    return sum;
}

I also tried doing it this way.

int sum(int* theArray, unsigned int arraySize)
{

    if (theArray = NULL)
    {
        throw ArrayException("NULL ARRAY REFERENCE");
    }
    else
    {
        int sum = 0;
        for (int i = 1; i < arraySize; i++)
        {
            sum += theArray[i];
        }
        return sum;
    }
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
Sean
  • 1
  • 1
  • 3
  • 2
    The problems is you are catch `int*` instead of ArrayException, by the way is much better if you exception class inherit from std::exception to have the basic functionality of exception. – NetVipeC Oct 01 '15 at 21:32
  • @NetVipeC, no, not because of that (there is honetsly not much functionality within them). It is better because this way the exception might be caught by reference to those basic types, and applications are supposed to have the catch for those by default. – SergeyA Oct 01 '15 at 21:35
  • 5
    Please don't try to learn c++ from video tutorials. That's the worst way to go. Go reading a good book, and leave it at your desk as reference you can come back every time you need. – πάντα ῥεῖ Oct 01 '15 at 21:40
  • `theArray=NULL` is an assignment, not a comparison, and it's never true. – molbdnilo Oct 01 '15 at 21:51

2 Answers2

3

While the post does not specifically mention it, I take it that the question is why exception is not caught? The answer is simple - because exception thrown is of type ArrayException, and catch is done with the type int*.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
  • Thank you for the time and responses. I thought that the catch is arbitrary and didn't have to be a string – Sean Oct 01 '15 at 21:53
  • @Sean - You don't want to catch a string, either. You want to `catch (ArrayException param)` – owacoder Oct 01 '15 at 22:03
0

The best way to get a grip on this stuff is as πάντα ῥεῖ recommended: get a good book. Here's a good place to start selecting books: The Definitive C++ Book Guide and List

The rest is a code block with comments where I figured they were needed.

#include <iostream>
// removed the using namespace std;
struct ArrayException
{
    ArrayException(std::string newMessage = "error") :
            message(newMessage)
    {
    }

    std::string message;
};

int sum(int* theArray, size_t arraySize) // change made here:
    // size_t better suited than unsigned int for indexes
{
    //throw std::out_of_range("BOOM!"); //uncomment to trigger a std::exception
    //throw 42; // uncomment to trigger the unknown exception

    if (theArray == NULL)
    {
        throw ArrayException("NULL ARRAY REFERENCE"); //perfect!
    }
    else
    {
        int sum = 0;
        for (size_t i = 0; i < arraySize; i++) // changes made here: 
            // size_t not int to get rid of signed/unsigned conflict
            // starting with array index 0, not 1
        {
            sum += theArray[i];
        }
        return sum;
    }
}

int main()
{
    try
    {
        sum (NULL, 10); // NULL address to force the exception
    }
    catch (ArrayException & param) // catch the custom exception
        // catch by reference where possible
    {
        std::cout << "This bad stuff happened: " << param.message << std::endl;
    }
    // extra stuff to show what can also be done
    catch (std::exception & stdexcpt) // catch any standard exceptions and 
                                  // print their message
    {
         std::cout << "Standard exception thrown: " << stdexcpt.what() << std::endl;
    }
    catch (...) // catch anything else that was thrown and doesn't 
                // correspond to any expectation 
    {
         std::cout << "No idea what happened." << std::endl;
    }
}
Community
  • 1
  • 1
user4581301
  • 33,082
  • 7
  • 33
  • 54