title: Java: Writing Exceptions
body: <p>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..)</p>
  
  <p>Entry class:</p>
  
  <pre><code>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 = "???";
      }
    }
  </code></pre>
  
  <p>AddressBook class:</p>
  
  <pre><code>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";
    }
  </code></pre>
  
  <p>I have searched the internet and found the following easy to follow websites:
  <a href="http://www.tutorialspoint.com/java/java_exceptions.htm" rel="nofollow">http://www.tutorialspoint.com/java/java_exceptions.htm</a>
  <a href="http://www.c-sharpcorner.com/UploadFile/433c33/defining-your-own-exception-class-in-javacustom-exception/" rel="nofollow">http://www.c-sharpcorner.com/UploadFile/433c33/defining-your-own-exception-class-in-javacustom-exception/</a></p>
  
  <p>Is this what I need to be doing? (new to java sorry)</p>