www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Segfault (NullPointerException) in Linux

reply Qian Xu <quian.xu stud.tu-ilmenau.de> writes:
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
next sibling parent Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
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
prev sibling parent reply Frank Benoit <keinfarbton googlemail.com> writes:
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
parent reply Qian Xu <quian.xu stud.tu-ilmenau.de> writes:
Frank Benoit wrote:
 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(); }
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.com
Mar 18 2009
parent Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Wed, Mar 18, 2009 at 6:44 PM, Qian Xu <quian.xu stud.tu-ilmenau.de> wrot=
e:
 Frank Benoit wrote:
 Qian Xu schrieb:
 ----------- code 2 (current solution) --------------
 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(); }
Hi, I have tried your code But gdc said, "Error: no property 'isNull' for type 'NullPointerExceptionTest.MyObj' Did I miss something?
He's talking about this, I think: http://en.wikipedia.org/wiki/Null_Object_pattern
Mar 18 2009