From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,19c9b8adc049bc94 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!news.glorb.com!wn11feed!worldnet.att.net!bgtnsc05-news.ops.worldnet.att.net.POSTED!53ab2750!not-for-mail Newsgroups: comp.lang.ada Subject: Re: handling null pointers/records to functions From: Jim Rogers References: <41274733@dnews.tpgi.com.au> Organization: Your Company Message-ID: User-Agent: Xnews/5.04.25 Date: Sat, 21 Aug 2004 13:55:14 GMT NNTP-Posting-Host: 12.73.184.124 X-Complaints-To: abuse@worldnet.att.net X-Trace: bgtnsc05-news.ops.worldnet.att.net 1093096514 12.73.184.124 (Sat, 21 Aug 2004 13:55:14 GMT) NNTP-Posting-Date: Sat, 21 Aug 2004 13:55:14 GMT Xref: g2news1.google.com comp.lang.ada:2910 Date: 2004-08-21T13:55:14+00:00 List-Id: "quasar" wrote in news:41274733@dnews.tpgi.com.au: > Hi , I was wondering what is the best way to handle null > records/pointers passed to functions with a return type that is > generic? > > for example in this snippet of code node_ptr_type is a record holding > a single data value of type Data_Type > > function Data_Part (Node_Ptr : Node_Ptr_Type ) return Data_Type is > begin > if node_ptr /= null then > return Node_Ptr.Data; > else > Put_Line("Node passed to FUNCTION DATA_PART is null"); > {(**)at this point i do not know what to do... do I terminate > the > program, do I return some form of null object} > end if; > end Data_Part; > > What is the most recommended way to handle this? Point (**) has got me > stuck. > > First, it is good to start thinking in Ada and leave some of the C and C++ terms for those languages. Node_Ptr_Type is an access type, not a pointer type. You have probably noticed that the compiler requires you to define a return value no matter what path is taken through the conditional expression. What should you consider as options? It appears that this function returns a value from a private data type. In this case, passing a null value to the function is an error. You can handle that error by raising an exception, which should be handled in the calling context. The exception clearly and unavoidably identifies an erroneous condition to the calling context. If the calling context must handle the exception unless you want this error to terminate the program. Any unhandled exception will terminate your program. You ask about returning some kind of null object. I suggest what you might consider here is defining some value of the Data member to indicate no data. This approach avoids exception handling but is easier to get wrong at the calling context. The calling context must compare all return values with the "no data" value before proceeding. Failure to do so will propogate bad data throughout the rest of the program. The use of exceptions is really much more reliable in this case. Jim Rogers