Is it possible to extend an already existing class to throw an exception? For example, is it possible to create an exception for the following code if the entry already exists? (I am unable to modify the class hence why I need to extend and have to include error handling rather than just displaying messages..)
Entry class:
public Entry(String paramString1, String paramString2, String paramString3, String paramString4, String paramString5)
{
this.firstName = paramString1;
this.lastName = paramString2;
this.street = paramString3;
this.town = paramString4;
if (paramString5.matches("[A-Z]{2}[0-9]{1,2} [0-9]{1,2}[A-Z]{2}")) {
this.postCode = paramString5;
} else {
System.err.printf("Bad postcode: '%s'\n", new Object[] { paramString5 });
this.postCode = "???";
}
}
AddressBook class:
public String add(Entry paramEntry)
{
if (paramEntry == null)
return "Error: null entry";
if (this.data.contains(paramEntry)) {
return "Error: this entry already in the book";
}
boolean bool = this.data.add(paramEntry);
if (bool) {
return " entry added";
}
return "entry could not be added";
}
I have searched the internet and found the following easy to follow websites: http://www.tutorialspoint.com/java/java_exceptions.htm http://www.c-sharpcorner.com/UploadFile/433c33/defining-your-own-exception-class-in-javacustom-exception/
Is this what I need to be doing? (new to java sorry)