I used spring boot to develop a shell poject used to send email, e.g.
sendmail -from foo@bar.com -password foobar -subject "hello world" -to aaa@bbb.com
from
password
noreply@bar.com
123456
from
password
if ((from != null && password == null) || (from == null && password != null)) {
throw new RuntimeException("from and password either both exist or both not exist");
}
There is a way using ^
(XOR) operator:
if (from == null ^ password == null) {
// use RuntimeException if you need to
throw new IllegalArgumentException("message");
}
if
condition will be true if only one variable is null.
But I think usually it's better to use two if
conditions with different exception message. You can't define what went wrong using single condition.