public class Person{
private String email;
public Person() throws Exception {
setEmail("info@yourmail.com");
}
public Person(String email) throws Exception {
setEmail(email);
}
public void setEmail(String email) throws Exception {
if (email.indexOf('@') == -1 || (email.substring(email.length() - 4) != ".com" && email.substring(email.length() - 3) != ".nz")) {
throw new Exception("[ERROR] Email not correct");
} else {
this.email = email;
}
}
However, initialization always fails when I test it this way:
void init() {
try{
person = new Person("example@mail.com");
}catch(Exception e) {
fail("Parameterized constructor failed");
}
}
I am not able to figure why. Why is this happening?