Catching Iterator Problems?

Hello Folks:

Developing in Windows 10 Pro.

I'm not a beginner, but I haven't done much with writing my own exceptions.

I'd like to catch exceptions from improper use of iterators.

Here is a piece of code that doesn't properly initialize an iterator.

An exception is thrown when I try to test this iterator against the end of a list.

Windows pops up the following Exception Unhandled window:

---------------------
Unhandled exception at 0x0F25ED76 (ucrtbased.dll) in net_results.exe: An invalid parameter was passed to a function that considers invalid parameters fatal.
---------------------

The execution stops.

I'd like to handle the exception and continue with program execution.

I wrap the code that causes the window to pop up in a try - catch clause but the window pops up and the execution stops anyway.

How would I catch an exception in the following flawed function?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
typedef std::list <int> INT_LIST_TYPE;

void iterator_exception_test()
{
	INT_LIST_TYPE::const_iterator int_iterator;
	INT_LIST_TYPE int_list;

	try
	{
		if (int_iterator == int_list.end())
		{
			int test_x = 0;
		}
		else
		{
			int test_x = 0;
		}
	}
	catch (...)
	{
		int test_x = 0;
	}
}
It's not a C++ exception, it's an SEH exception, so you can't catch it with standard C++ exception handling.

https://msdn.microsoft.com/en-us/library/swezty51.aspx?f=255&MSPPError=-2147217396

Needless to say, this works only on Windows. Other platforms may just kill your program outright without any exceptions, or they may throw regular exceptions. Strictly speaking, the behavior is undefined. You really should just fix the code, rather than rely on exception handling.
If you want checked accesses, use std::x::at() when available.
Then you may catch std::out_of_range.

In general, there isn't any point to continuing once you've established that your program is broken...
Last edited on
Topic archived. No new replies allowed.