Reputation: 4025
I got the following code:
set<Object*>::iterator it;
try
{
for (it = SetOfObjects->begin(); it != SetOfObjects->end(); ++it)
{
//some actions, not applicable to the question
}
}
catch(...)
{
this->m_error_raiser->error_Name = "Station isn`t connected to Object! Use connectToObject method or list of forecast objects is empty";
this->m_error_raiser->error_Number = 101;
//throw (this->m_error_raiser);
}
When instance of SetOfObjects is not created and I am trying to iterate through that set I got expected run- time error.
So I decided to handle that error and give info about it to the user by means of try catch.
My question: although I catch all exceptions thus they are considered handled, my program still terminates during run -time which contradicts to it`s behavior which I expect from it: it should continue to work because all generated exceptions were handled. What is wrong here?
Upvotes: 2
Views: 434
Reputation: 1300
In Windows environment you technically can catch low-level exceptions like this (dereferencing null/uninitialized pointer) - SEH exceptions. This is done by using Microsoft-specific __try() and __except() statements.
This may be useful, if you have an external not-so-well-written library, which crashes (follows null pointer, etc..) instead of reporting error i.e. when file is not found.
But, as already mentioned in comments, using this in your code is not portable. And they are not interoperable with C++ exceptions. So even if you decide to use them you'll end up with spagetti of 2 exception handling mechanisms... A bad design probably)
However, if youe code relies on exception-handling for error-reporting, you can always make a null check and throw a custom exception on failure: if(pointer==NULL) throw something;
Upvotes: 2
Reputation: 55897
If object is pointer and it's not initialized, usage of such object is undefined behaviour
. You can't handle usage of such pointer by exception handling
(by standard). Only initialize to 0 by default and verify that pointer is not null
before usage.
Upvotes: 3