Java to D - exception

webdev fake at test.com
Wed Sep 23 16:53:05 UTC 2020


Trans some Java code to D, so far has lots of issues, this time I 
will focus on Exception.

interface Widget {
	string name();
}

Widget createWidget() {
	return null;
}

void test() {
	createWidget().name();
}

The similar code in Java will throw NullPointerException, but in 
D it
will throw object.Error, how do I know it was caused by null?

And is it possible to override callback like onOutOfMemoryError & 
onRangeError?

In Java you can catch multi exception like

try {
	...
} catch (A | B e) {
	...
}

can trans to:
try {
	...
} catch (A e) {
	...
} catch (B e) {
	...
}

or:
try {
	...
} catch (Throwable t) {
	if (t is A | B) {
		...
	} else {
		throw t;
	}
}

which one is better, will it has side effect?


More information about the Digitalmars-d mailing list