Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can AddressValueException(String message) be made public? #127

Open
ryancdotorg opened this issue May 11, 2024 · 2 comments
Open

Can AddressValueException(String message) be made public? #127

ryancdotorg opened this issue May 11, 2024 · 2 comments

Comments

@ryancdotorg
Copy link

I am subclassing IPv4Address and IPv6Address, and I would like to be able to throw a subclass of AddressValueException with a custom error message.

Can you please make AddressValueException(String message) so I can do this?

@seancfoley
Copy link
Owner

At first glance, this seems reasonable for the next release. I presume you are using version 5.x.x.

In the meantime, you can use this trick:

 throwAddressValueException("my string");
	
public static AddressValueException throwAddressValueException(String arg) throws AddressValueException {
	Class<AddressValueException> clazz = AddressValueException.class;
	try {
		Constructor<AddressValueException> constructor = clazz.getDeclaredConstructor(String.class);
		constructor.setAccessible(true);
		AddressValueException instance = constructor.newInstance(arg);
		throw instance;
	} catch( NoSuchMethodException | SecurityException | InstantiationException | 
			IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
		throw new Error(e);
	}
}
Exception in thread "main" inet.ipaddr.AddressValueException: my string
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
	at issue127.Issue.throwAddressValueException(Issue.java:19)
	at issue127.Issue.main(Issue.java:11)

@ryancdotorg
Copy link
Author

Yes, I'm using 5.5.0 - the block splitting and merging functionality is great.

Thanks for the reflection example to tide me over, that will work for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants