www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Access Vialotation

reply Qian Xu <quian.xu stud.tu-ilmenau.de> writes:
Hi All,

Is there any way to keep program alive, when an AV takes place?

-- demo -------------------------------
module NullPointerExceptionTest;

class Foo {
  void bar() {}
}

void main() {
  Foo foo; // foo is still NULL
  try {
    foo.bar(); // A NullPointerException will be thrown
  }
  catch (Exception E) {
    // Can I catch this NullPointerException?
  }
}
----------------------------------------

The program will crash, when the line "foo.bar()" is executed, even when a
try-catch-block is added. So far as I know, Java and Delphi can prevent
such kind of crashes from happening with a try-catch-block. Is it possible
in D?

Best regards
--Qian
Feb 27 2009
next sibling parent Qian Xu <quian.xu stud.tu-ilmenau.de> writes:
UPDATE: I am using gdc compiler in Linux
Feb 27 2009
prev sibling parent reply Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Fri, Feb 27, 2009 at 9:01 AM, Qian Xu <quian.xu stud.tu-ilmenau.de> wrot=
e:
 Hi All,

 Is there any way to keep program alive, when an AV takes place?

 -- demo -------------------------------
 module NullPointerExceptionTest;

 class Foo {
 =A0void bar() {}
 }

 void main() {
 =A0Foo foo; // foo is still NULL
 =A0try {
 =A0 =A0foo.bar(); // A NullPointerException will be thrown
 =A0}
 =A0catch (Exception E) {
 =A0 =A0// Can I catch this NullPointerException?
 =A0}
 }
 ----------------------------------------

 The program will crash, when the line "foo.bar()" is executed, even when =
a
 try-catch-block is added. So far as I know, Java and Delphi can prevent
 such kind of crashes from happening with a try-catch-block. Is it possibl=
e
 in D?
It's possible on Windows in D, but that's because Windows reports segfaults with the same mechanism that D uses for exceptions. Since Linux (and many other Posix systems) uses signals, it's probably very tricky to implement in a cross-platform manner.
Feb 27 2009
parent reply BCS <ao pathlink.com> writes:
Reply to Jarrett,

 On Fri, Feb 27, 2009 at 9:01 AM, Qian Xu <quian.xu stud.tu-ilmenau.de>
 wrote:
 
 Hi All,
 
 Is there any way to keep program alive, when an AV takes place?
It's possible on Windows in D, but that's because Windows reports segfaults with the same mechanism that D uses for exceptions. Since Linux (and many other Posix systems) uses signals, it's probably very tricky to implement in a cross-platform manner.
you can have a posix signal handler throw an exception (I have done it and had it work) but I have no idea if it is supported.
Feb 27 2009
next sibling parent Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Fri, Feb 27, 2009 at 2:31 PM, BCS <ao pathlink.com> wrote:
 you can have a posix signal handler throw an exception (I have done it and
 had it work) but I have no idea if it is supported.
Yeah, that seems dubious to me. I don't know what kinds of guarantees, if any, the signal handler has as to what thread it's called in (if any) etc.
Feb 27 2009
prev sibling parent reply downs <default_357-line yahoo.de> writes:
BCS wrote:
 Reply to Jarrett,
 
 On Fri, Feb 27, 2009 at 9:01 AM, Qian Xu <quian.xu stud.tu-ilmenau.de>
 wrote:

 Hi All,

 Is there any way to keep program alive, when an AV takes place?
It's possible on Windows in D, but that's because Windows reports segfaults with the same mechanism that D uses for exceptions. Since Linux (and many other Posix systems) uses signals, it's probably very tricky to implement in a cross-platform manner.
you can have a posix signal handler throw an exception (I have done it and had it work) but I have no idea if it is supported.
In my experience, that works exactly once.
Feb 28 2009
next sibling parent Christopher Wright <dhasenan gmail.com> writes:
downs wrote:
 BCS wrote:
 Reply to Jarrett,

 On Fri, Feb 27, 2009 at 9:01 AM, Qian Xu <quian.xu stud.tu-ilmenau.de>
 wrote:

 Hi All,

 Is there any way to keep program alive, when an AV takes place?
It's possible on Windows in D, but that's because Windows reports segfaults with the same mechanism that D uses for exceptions. Since Linux (and many other Posix systems) uses signals, it's probably very tricky to implement in a cross-platform manner.
you can have a posix signal handler throw an exception (I have done it and had it work) but I have no idea if it is supported.
In my experience, that works exactly once.
So you can use the signal handler to print a stack trace and maybe some context, but throwing an exception that you then catch is dangerous.
Feb 28 2009
prev sibling parent reply BCS <none anon.com> writes:
Hello downs,

 BCS:

 you can have a posix signal handler throw an exception (I have done
 it and had it work) but I have no idea if it is supported.
 
In my experience, that works exactly once.
That would match with what I needed: poor man's stack tracing int EveryFunction() { scope(failure) writef("%s:%s\n",__FILE__,__LINE__); ... }
Feb 28 2009
parent Qian Xu <quian.xu stud.tu-ilmenau.de> writes:
BCS wrote:
 Hello downs,
 
 BCS:

 you can have a posix signal handler throw an exception (I have done
 it and had it work) but I have no idea if it is supported.
In my experience, that works exactly once.
That would match with what I needed: poor man's stack tracing int EveryFunction() { scope(failure) writef("%s:%s\n",__FILE__,__LINE__); ... }
it does not work with gdc. d2.0 does not have problem with NullPointerException at all. -- Xu, Qian (stanleyxu) http://stanleyxu2005.blogspot.com
Mar 03 2009