Handling exceptions

Maybe it is an obvious question, but I need to ask help for it.

I need to know how to handle exceptions in OCC.
User input can crash my application, for instance, making a segment with equal points.

My first approach was:
if (!GC_MakeSegment(pA, pB)) return false;
expecting a Null value, but app crashes.

My second approach was:
GC_MakeSegment(pA, pB)
catch (...) //Or even (Standard_Failure e)

Nothing to do, crashes any way.

So I have to think about possible problems in my code and to be ready for those, such as

if (pA == pB) return false;
GC_MakeSegment(pA, pB)

but there is no operator= for gp_Pnt.
My last try is
if ((pA.X()==pB.X()) && (pA.Y()==pB.Y()) && (pA.Z()==pB.Z())) return false;

I hope (I am sure) there are better ways to manage exceptions.

Could you give me some advises to start with it?
I tried to search with Exception, Try, Catch, keywords in the forum but I always find topics nothing related with my subject.


Roman Lygin's picture

Hi arkoala,

Read Foundation Classes User's Guide. There is a chapter dedicated to exception handling explaining details.
You have to protect against exception with catch(Standard_Failure) or with descendants of Standard_Failure for more specific cases. However you should also/rather verify user's input before passing them into OCC algorithms.

To compare gp_Pnt use gp_Pnt::IsEqual():

if (!pA.IsEqual (pB, Precision::Confusion()) {
//create a segment

In general, never compare floating point numbers for absolute equality, rather use some tolerance/precision (e.g. if (abs (a-b) < myEpsilon) ...). In OCC use Precision methods:
for 3D space use Precision::Confusion(), for 2D parametric space - Precision::PConfusion(), for instance:
if (abs (myParameter1 - myParameter2) > Precision::PConfusion()) {...}

Hope this helps.

opencascade.blogspot.com - blog on Open CASCADE

haowei's picture

GC_MakeSegment(pA, pB);


arkoala's picture

Ok, app continues crashing, but it is good to know your possibilities.

I will check points for this function with IsEqual. It is a shame function doesn't make this check into the body.

I only knew about this forum and bottle tutorial in the web.
I have just found doc directory with your documentation related so I have a lot of things to read now.

Thank you very very much.