Ok, so this is not allowed:
public class ServiceError extends RuntimeException {
private final UUID uuid = UUID.randomUUID();
public ServiceError(...) {
super("{" + uuid.toString() + "} " + ...); // error on reference to uuid
}
But how can I do what I want? There is no Exception.setMessage
, so I can't change the message after the super constructor has been completed, I need to pass the UUID in the constructor.
I would do this:
public class ServiceError extends RuntimeException {
private final UUID uuid;
public ServiceError() {
this(UUID.randomUUID());
}
private ServiceError(UUID uuid) {
super("{" + uuid.toString() + "} " + ...);
this.uuid = uuid;
}
}
Basically this is just a matter of changing where the UUID.randomUUID()
call is made, and using a constructor parameter both to construct the superconstructor argument and to save the value to the field afterwards.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With