www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why null reference not showing crash.

reply Brother Bill <brotherbill mail.com> writes:
 From page 233 of "Programming in D".
```
import std.stdio;
import std.exception;

void main() {
	MyClass variable;
	use(variable);
}

class MyClass {
	int member;
}

void use(MyClass variable) {
	writeln("variable: ", variable);
	
	try {
		writeln(variable.member); // ← BUG
	} catch (Exception ex) {
		writeln("Exception: ", ex);
	}
}
```

Why does this run, but not display expected null reference 
exception?
Aug 02
next sibling parent monkyyy <crazymonkyyy gmail.com> writes:
On Saturday, 2 August 2025 at 20:29:22 UTC, Brother Bill wrote:
 From page 233 of "Programming in D".
 ```
 import std.stdio;
 import std.exception;

 void main() {
 	MyClass variable;
 	use(variable);
 }

 class MyClass {
 	int member;
 }

 void use(MyClass variable) {
 	writeln("variable: ", variable);
 	
 	try {
 		writeln(variable.member); // ← BUG
 	} catch (Exception ex) {
 		writeln("Exception: ", ex);
 	}
 }
 ```

 Why does this run, but not display expected null reference 
 exception?
Not all errors are exceptions, Expection is just a class in the std, and then theres Error which is "more important" and `catch(Error)` gets you some extra cases, you can define your own. etc. For most cases of `try` to do anything someone had to write a literal `Throw` but I think the os kills you before you even passed something to writeln. Fundmentally youd have to have *every* access of a class be null checked, which I bet people want, but must not be part of the 30 year old c compiler that d comes from.
Aug 02
prev sibling next sibling parent "Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> writes:
It does die from the segfault.

Program terminated with signal: SIGSEGV

Note: by default D does not throw an exception (it would be an Error not 
Exception).

There is some code to do this for linux, and we've approved if someone 
is willing to implement it, a read barrier to check for null deref 
behind a CLI switch.

Unfortunately right now, segfaults in D may or may not generate a stack 
trace, it depends upon the platform and if you've got debug info 
compiled in.

For posix systems you have to inspect the core dump of the process after 
it dies. What to do for this isn't D specific, and you can find 
tutorials for it by platform.
Aug 02
prev sibling parent Andy Valencia <dont spam.me> writes:
On Saturday, 2 August 2025 at 20:29:22 UTC, Brother Bill wrote:
 Why does this run, but not display expected null reference 
 exception?
Death by signal (SIGSEGV in this case) is not an Exception. I had a program dying from a SIGPIPE--same deal, you can't catch it with an exception handler. Fortunately for me, SIG_IGN the SIGPIPE was the right thing. In your case, SIG_IGN is out of the question. I guess you could arm a SIGSEGV handler, but I doubt this is useful in any but the most uniquely manageable scenarios. Andy
Aug 02