Segfault (NullPointerException) in Linux

Qian Xu quian.xu at stud.tu-ilmenau.de
Wed Mar 18 08:21:09 PDT 2009


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


More information about the Digitalmars-d-learn mailing list