Java Try Catch

Kua

Kua

Associate
Joined
21 Jul 2008
Posts
512
Location
Lancaster
There are no errors but the driver won't enter the try block at the bottom. "Test" should print to the console, but it doesn't. I.e. the Employees aren't instantiated.

Code:
public Employee(String name, String gender, String DOB, String jobTitle, int salary) throws InvalidDateException{
		employeeName = name;
		employeeGender = gender;
		employeeDOB = DOB;
		employeeJobTitle = jobTitle;
		employeeSalary = salary;
		
		//Some problem with using this if statement
		if(validDOB(DOB)==false){
			throw new InvalidDateException("Please enter date in format: DD/MM/YYYY");
		}
	}

public boolean validDOB(String date){
		if(date.matches("[0-9]{2}/[0-2][0-9]/[0-9]{4}")){
			return true;
		}
		return false;
		
	}

Code:
public class Driver {
	
	public static void main(String[] args) {
		
		
		
		//Create space in memory and initialise *fields*
		//WHY WON'T IT ENTER THE TRY-CATCH??
		
		try {
			Employee fred = new Employee("Fred Flinstone", "Male", "02/02/1960", "Manager", 25250);
			Employee lisa = new Employee("Lisa Simpson", "Female", "09/05/1981", "Sales Staff", 19750);
			Employee charlie = new Employee("Charlie Brown", "Male", "03/11/1950", "Sales Staff", 17500);
			System.out.println("test");
		} catch(InvalidDateException e) {
			e.getMessage();
		}
}
}
 
Yes. You were all bang on. I was rather stupidly thinking e.getMessage() printed to the console, but it just returns a String.

Noob question: How can you tell I'm using Eclipse?
 
Back
Top Bottom