Try/Catch

Does the catch section of a try catch have access to variables within the try section? In the following example how does the catch section delete the list? Does it have access to the head?

Also am I using the right language when referring to the try and catch sections as sections?

1
2
3
4
5
6
7
8
9
 void procD()
try {	Node*	head;
	// Creates a linked list
	// Some_exception thrown here
}
catch(Some_exception)
{	// Delete list
	throw;	// rethrow exception for procC to deal with
}
Does the catch section of a try catch have access to variables within the try section?

No, it hasn't.

In the following example how does the catch section delete the list?

It doesn't have access to it, so it can't delete it. The destructors of objects however are called when an exception is raised, so you can solve this problem for example by using smart pointers: http://en.wikipedia.org/wiki/Smart_pointer

Also am I using the right language when referring to the try and catch sections as sections?

It doesn't really matter much, but in C++ 'try and catch blocks' are used mostly.
OK thanks a lot Fransje. You wouldn't have any idea what my lecturer was trying to get at then?

He mentions 2 ways to deal with dealing with a memory leak like above when throwing an exception, the one above and, as you mentioned. a destructor of an linkedlist object,
Last edited on
Topic archived. No new replies allowed.