digitalmars.D.learn - Segfault (NullPointerException) in Linux
- Qian Xu (32/32) Mar 18 2009 Hi All,
- Jarrett Billingsley (4/11) Mar 18 2009 If you're getting a segfault, your program is broken. You shouldn't
- Frank Benoit (9/21) Mar 18 2009 If you want to be able to return something like "nothing", you can use
- Qian Xu (8/30) Mar 18 2009 Hi, I have tried your code
- Jarrett Billingsley (4/33) Mar 18 2009 He's talking about this, I think:
Hi All, again to the topic "Segfault (NullPointerException) in Linux" Is it really impossible to catch NullPointerException (segfault) using try-catch-statement in Linux? I can use signal handler to catch it at system level. But my program will stop. If it can be captured inside the program. My program will be more robust. And I can write my code more flexible. ------------- code 1 (ideal) --------------------- public test(MyObj obj) { try { obj.getObj2.getObj3.test(); } except(E) { // NullPointerException was caught! } } ------------- code 2 (current solution) -------------- public test(MyObj obj) { if (obj !is null && obj.getObj2 !is null && obj.getObj2.getObj3 !is null) { obj.getObj2.getObj3.test(); } } ------------------------------------------------------ Best regards --Qian
Mar 18 2009
On Wed, Mar 18, 2009 at 11:21 AM, Qian Xu <quian.xu stud.tu-ilmenau.de> wrote:Hi All, again to the topic "Segfault (NullPointerException) in Linux" Is it really impossible to catch NullPointerException (segfault) using try-catch-statement in Linux?Yeah, it is impossible.I can use signal handler to catch it at system level. But my program will stop. If it can be captured inside the program. My program will be more robust. And I can write my code more flexible.If you're getting a segfault, your program is broken. You shouldn't be catching and handling them anyway.
Mar 18 2009
Qian Xu schrieb: ----------- code 2 (current solution) --------------public test(MyObj obj) { if (obj !is null && obj.getObj2 !is null && obj.getObj2.getObj3 !is null) { obj.getObj2.getObj3.test(); } } ------------------------------------------------------If you want to be able to return something like "nothing", you can use NullObject. That is, return an object that lets you navigate the references and lets you test for it being a "null" object. auto o = obj.getObj2.getObj3; if( !o.isNull() ){ o.test(); }
Mar 18 2009
Frank Benoit wrote:Qian Xu schrieb: ----------- code 2 (current solution) --------------Hi, I have tried your code But gdc said, "Error: no property 'isNull' for type 'NullPointerExceptionTest.MyObj' Did I miss something? -- Xu, Qian (stanleyxu) http://stanleyxu2005.blogspot.compublic test(MyObj obj) { if (obj !is null && obj.getObj2 !is null && obj.getObj2.getObj3 !is null) { obj.getObj2.getObj3.test(); } } ------------------------------------------------------If you want to be able to return something like "nothing", you can use NullObject. That is, return an object that lets you navigate the references and lets you test for it being a "null" object. auto o = obj.getObj2.getObj3; if( !o.isNull() ){ o.test(); }
Mar 18 2009
On Wed, Mar 18, 2009 at 6:44 PM, Qian Xu <quian.xu stud.tu-ilmenau.de> wrot= e:Frank Benoit wrote:He's talking about this, I think: http://en.wikipedia.org/wiki/Null_Object_patternQian Xu schrieb: ----------- code 2 (current solution) --------------Hi, I have tried your code But gdc said, "Error: no property 'isNull' for type 'NullPointerExceptionTest.MyObj' Did I miss something?public test(MyObj obj) { =A0if (obj !is null && =A0 =A0 =A0obj.getObj2 !is null && =A0 =A0 =A0obj.getObj2.getObj3 !is null) =A0{ =A0 =A0 obj.getObj2.getObj3.test(); =A0} } ------------------------------------------------------If you want to be able to return something like "nothing", you can use NullObject. That is, return an object that lets you navigate the references and lets you test for it being a "null" object. auto o =3D obj.getObj2.getObj3; if( !o.isNull() ){ =A0 =A0o.test(); }
Mar 18 2009