I've got a class cnMeshHelper whose method cnMeshHelper::initialize calls various functions that may throw cnZeroDivisionException.
The stripped down code looks like this:
cnVector cnVector::getNormalized() {
if (getLength() == 0.0) {
#ifdef DEBUG
printf("Throwing exception in cnVector::getNormalized().\n");
#endif // DEBUG
throw cnZeroDivisionException();
}
// ...
}
cnMatrix cnMatrix::getNormalized() {
return cnMatrix(
T.getNormalized(),
B.getNormalized(),
N.getNormalized(),
M.getNormalized() );
}
cnMatrix cnComputeTangentSpace( /* ... */ ) {
cnMatrix mat;
// ...
mat.T = mat.T.getNormalized()
return mat;
}
void cnMeshHelper::initialize(cnMesh& mesh) {
// ...
polygonMatrix = cnComputeTangentSpace( /* ... */ );
}
int main() {
cnMesh* mesh = new cnMesh( /* ... */ );
// ...
try {
cnMeshHelper helper;
helper.initialize(*mesh);
}
catch(cnZeroDivisionException& e) {
cout << "Zero division exceptione caught.\n";
}
}
Now, in some cases, it can happen that the cnMesh* mesh has invalid values that cause to throw an exception in cnVector::getNormalized().
But the problem is, if this is the case, the try-catch clause does not catch the exception. The program just terminates after printing the DEBUG-text in the method above.
Update: Note that I can catch the exception within cnMeshHelper::initialize() but everything "outer" doesn't work.
Even using catch(...)
does not work in the main.
Can you tell me why the exception isn't caught ?
Got it !
It happens often, that the issue lies somewhere else. So in this case, too.
The cnMeshHelper destructor throwed a null-pointer because of an uninitialized member. The member was uninitialized because of the cnZeroDivisionException in cnMeshHelper::initialize.
Thanks for you help, anyway.