Throwing Exception, Segmentation Fault

Oct 10, 2013 at 6:40pm
The below method causes a segmentation fault. The method is intended to insert something into some position of a linked list. It works as long as the first if statement is true. If not, it gives a segmentation fault, even though I threw an exception in the else case. What is going on?

void hello::addbeforeIth(int I, el_t newNum)
{
int i;
i = 0;

if (!(I<1||I>Count+1))
{

if (isEmpty()||I==Count+1)
{
addRear(newNum);//References rear
return;
}

if (I == 1)
{
addFront(newNum);
return;
}

else
{
Node *p;
Node *q;
p = Front;
q = Front;

while (i<I)
{
p = p->Next;
i++;
}
i = 0;
while (i<(I-1))
{
q = q->Next;
i++;
}
Node *r;
r = new Node;
r->Elem = newNum;
q->Next = r;
r->Next = p;//qrp
Count++;//Count line
}
}
else{throw OutOfRange();}
}

EDIT: It throws the exception unless you do it in a try block. I'm doing this:

try
{
l.addbeforeIth(9,12);
}
catch (hello::OutOfRange)
{cerr << "Error has occurred: out of range"; }

If there's an error, we're not going to exit; we'll just move on.
Last edited on Oct 10, 2013 at 6:51pm
Oct 10, 2013 at 6:48pm
How OutOfRange function defined?
Oct 10, 2013 at 6:53pm
class hello
{
...


public:


class OutOfRange{};
...
}

EDIT: the try block is in the client file, and addbeforeIth is in the .cpp
Last edited on Oct 10, 2013 at 6:56pm
Oct 10, 2013 at 6:59pm
is it he whole class out of range or it is something inside it? Stripped-down example works for me.
Also try to capture by reference, not value
Last edited on Oct 10, 2013 at 6:59pm
Oct 10, 2013 at 7:03pm
That is the full definition of OutOfRange. I left nothing out.
Oct 10, 2013 at 7:19pm
You could post ypur program on pastebin or, better if it is one file, compile it with ideone.com and post link here
Oct 10, 2013 at 7:41pm
In hello.cpp, the function we are looking at is at the very bottom. The part of the client file that uses it is in the method CaseTwo.

hello.h:
http://pastebin.com/ATk6kQZF
hello.cpp:
http://pastebin.com/y5C6RzsV
helloClient.C:
http://pastebin.com/rw96UNt6
Oct 10, 2013 at 7:48pm
There is a bunch of errors like unknown variable p in deleteRear and incorrect catch block in caseTwo. Also which case trows an error?
Oct 10, 2013 at 8:01pm
Yeah okay I forgot to declare p. That didn't cause problems yet as I didn't call it. But I will call it later so thanks. The incorrect catch block is the problem right now.

if (!(I<1||I>Count+1))
{
...}
else{throw OutOfRange();}
Oct 10, 2013 at 8:03pm
That didn't cause problems yet as I didn't call
It should cause compile errors.

The incorrect catch block is the problem right now.
I have askes which case should I select from your program main menu to see the crash.
Oct 10, 2013 at 8:11pm
Run case 2. Uncomment these lines. Add the try to the top of the block. It was in the original program but I accidentally missed copying it when I pasted from emacs.

This code is in casetwo

try
{
//l.addbeforeIth(9,12);
}
catch (hello::OutOfRange)
{cerr << "Error has occurred: out of range"; }
try
{
//l.addbeforeIth(0,0);
}
catch (hello::OutOfRange)
{cerr << "Error has occurred: out of range"; }
Last edited on Oct 10, 2013 at 8:12pm
Oct 10, 2013 at 8:19pm
1
2
3
          while (i<I)
            {
              p = p->Next;
When is is equal to count p wold be set to nullptr.
Then that line (OldNum = p->Elem;) will cause a fault
Oct 10, 2013 at 8:27pm
Right, but I thought that throwing an exception would stop the program from doing that. Because then we would go to the catch block.
Oct 10, 2013 at 8:29pm
throwing an exception
You are not trowing an exception here.
You have a program which tries to do very illegal operation so OS itself have to stop it.
Topic archived. No new replies allowed.