www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Problem with Exceptions in Constructor

reply Mike Brown <mikey.be gmail.com> writes:
Hi all,

AddressSanitizer is throwing an access-violation error - I've 
managed to boil it down to this example:

```d
import std.stdio;

class example: Exception {
	this(string msg, string file = __FILE__, size_t line = __LINE__) 
{
		super(msg, file, line);
	}
}

class test {
	this() {		
		throw new example("this is a test");
	}
}

void main() {
	try {
		auto t = new test();
	} catch (example e) {
		writeln(e.msg);
	}
}
```

Compiled with:
ldc2.exe -g -fsanitize='address' .\test.d -of='test.exe'

The error is flagged on the writeln(e.msg). Do I need to do 
something special to pass a string to an exception? dup?

Kind regards,
Mike Brown
Jun 11 2021
parent reply Mike Parker <aldacron gmail.com> writes:
On Friday, 11 June 2021 at 20:09:22 UTC, Mike Brown wrote:

 The error is flagged on the writeln(e.msg). Do I need to do 
 something special to pass a string to an exception? dup?
No, that code is fine. You're passing a string literal, so there's no need for a dup.
Jun 11 2021
parent Mike Brown <mikey.be gmail.com> writes:
On Saturday, 12 June 2021 at 02:19:21 UTC, Mike Parker wrote:
 On Friday, 11 June 2021 at 20:09:22 UTC, Mike Brown wrote:

 The error is flagged on the writeln(e.msg). Do I need to do 
 something special to pass a string to an exception? dup?
No, that code is fine. You're passing a string literal, so there's no need for a dup.
Hi Mike, Thank you - OK, I had another issue that was related to an out of date LDC2 and Druntime. But that now looks to be sorted. I have set up a VM of Linux Mint. The code doesn't raise an error on there. I guess this might be a bug in the windows release? I will log it Kind regards, Mike Brown
Jun 12 2021