X
PrevPrev Go to previous topic
NextNext Go to next topic
Last Post 26 Apr 2013 09:23 AM by  anon
how to fail an object's INIT
 3 Replies
Sort:
You are not authorized to post a reply.
Author Messages

anon



New Member


Posts:
New Member


--
26 Apr 2013 09:23 AM
    I read in the documentation that when the constructor fails, INIT should return 0 rhather than 1. However, this seems to pass undetected and the program continues, albeit with an invalid object pointer. Should the result of obj_new() always be tested for validity, and if so, what is the proper way to do so?

    Deleted User



    New Member


    Posts:
    New Member


    --
    26 Apr 2013 05:29 PM
    Yes, of course, it should always be checked. However, in practice, I've never seen it done. :-) Well, I have done it myself a couple of times, but I'm particularly anal about these kinds of things. I would do it like this, probably: object = MyObject() IF ~Obj_Valid(object) THEN RETURN I would rely on the error handling in the object INIT method to supply the reason for the failure.

    Deleted User



    New Member


    Posts:
    New Member


    --
    28 Apr 2013 07:48 AM
    Thanks, David. This makes sense. However, what's the purpose then of the return value? Wim

    Deleted User



    New Member


    Posts:
    New Member


    --
    28 Apr 2013 01:54 PM
    I agree with David. It's rare in code that I've seen or written to explicitly check the return state of an OBJ_NEW, unless you have a reason to suspect the creation may fail, for example if the initialization of the object uses up system resources. The main use case for checking a return that you'll see is in the ::Init of a class that subclasses from one or more other classes. This pattern is common throughout the IDL /lib classes. function mysubclass::init, _ref_extra = extra if (~self->mysuperclass::init(_extra = extra)) then return, 0 ... return, 1 end If you're something less than completely antisocial, you may also consider adding a CATCH block to your class' Init to alleviate the potential for leaking system resources, then return a 0.
    You are not authorized to post a reply.