* Watch the following videos and answer the questions.
* Complete the exercise at the end of this sheet.
## Java Exceptions
* Give examples of exceptions
* What is a stack trace?
* What does throws do?
* Why is catching an Exception bad practice?
<https://www.youtube.com/watch?v=QFGqM1H0bQ0>
# Handling Exceptions
* What's an off by one error?
<https://www.youtube.com/watch?v=-ZtCzzaRj1U>
## Exercise
Implement the example code below. Add the following member variables to the pupil class then add appropriate validation and exception handling
* Form (Of the form 7A, 7B, 7C, 7D, 7E up to 11E)
* Address
* Postcode
* Home Phone
* Mobile
* Replace the age field with Date of Birth. Validate so the pupil is between 11 and 18
## Example Implementation
```java
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Pupil p1 = new Pupil();
try {
p1.setName("Bill");
p1.setGender("N");
p1.setAge(11);
//db.add(pupil);
} catch (PupilException ex) {
System.err.println(ex);
}
}
}
public class Pupil {
private String name;
private String gender;
private int age;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the gender
*/
public String getGender() {
return gender;
}
/**
* @param gender the gender to set
*/
public void setGender(String gender)throws PupilException {
if(!gender.matches("M|F"))
{
throw new PupilException("Gender must be M or F");
}
this.gender = gender;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) throws PupilException {
if(age<11 || age >18) throw new PupilException("Age must be between 11 and 18");
this.age = age;
}
}
public class PupilException extends Exception{
PupilException(String ex)
{
super(ex);
}
}
```