CodeIEB
Theory Notes/🗄️ Topic 4: Data & Information Management, Solution Development/11.4.14
11.4.14Grade 11

Data Validation & Exception Handling

Coding the validation checks conceptually introduced in 11.2.7, plus using exception handling to gracefully manage runtime errors.

Reasons for data validation: to prevent erroneous/invalid data from ever entering the system in the first place, protecting the accuracy and reliability of the program's data.

Exception handling
A programming mechanism (try/catch in Java) that lets a program detect and gracefully respond to runtime errors, instead of crashing.

Example

try { int number = Integer.parseInt(userInput); } catch (NumberFormatException e) { System.out.println("Please enter a valid whole number."); }

Coding the full set of validation checks (from 11.2.7), typically using a loop that keeps prompting the user until valid input is provided:

CheckExample condition (pseudocode)
Presence checkinput != ""
Range checkvalue >= min AND value <= max
Uniqueness checkvalue not already in existing records
Length checkinput.length() == expectedLength
Type checkhandled via exception handling when parsing, e.g. catching NumberFormatException
Logical checkendDate > startDate
Check digitrecalculated digit matches the supplied check digit
Checksumrecalculated checksum matches the expected value

Example

// Range check with a validation loop int age; do { System.out.print("Enter age (0-120): "); age = scanner.nextInt(); } while (age < 0 || age > 120);

💡 Exam Tip

A validation loop should re-prompt until valid data is given, using a post-check loop (do-while) since you need to get the input at least once before you can check it.