# A-level Computing 2009/AQA/Problem Solving, Programming, Data Representation and Practical Exercise/Fundamentals of Programming/Validation
### Error types

When you write a program it often won't work as you expect. It might not compile, it might crash when you run it, or it might give you the wrong result. These are all errors with your code and we can place them into 3 different error categories:
* Compilation Errors
* Run-time Errors
* Logic Errors
Let's take a look at what each of them mean.

#### Compilation (Syntax) error

You have probably met this error a lot, when you try and run your program it won't compile, giving you an error message. If you are using something like Visual Studio it will even underline the problem code with a blue squiggly line. There is a problem with the structure, or syntax, of the code that you have written. This might be a situation where you have forgotten to add a closing bracket or you have misspelt a key word. Take a look at this example:
You should be able to see that in line 1 the programmer has misspelt the word to. This code won't work at all.

#### Run-time error

Sometimes you will have a program that compiles fine, but breaks when you actually run it. For example, this code here:
The programmer has created an infinite loop, and the value of total will head towards infinity, eventually breaking the program.

#### Logic (Semantic) error

A logic error is when a program compiles, doesn't crash, but the answers that it gives are incorrect. The logic, semantics or meaning, conveyed by the code is wrong. Take a look at the next example:
In the above example you would expect it to print out:
But because there is a logic error on line 6, it prints:
To fix it, you have to fix the logic of the code and change line 6 to:
* Exercise: Validation
* Name and give examples of the three error types in programming code:
* Answer:
* Compilation (Syntax)
* Logic (Symantic)
* Runtime
* What error is in the following code, how would you fix it:
* dim x as integer
* do until x > 5
* x = 1
* x = x + 1
* loop
* Answer:
* There is a Runtime error. x will never be greater than 5, since line 3 gives x the value of 1, thus, the loop will always end in 2 and the loop will never end. This could be fixed by moving the x = 1 instruction outside the loop, between line 1 and 2.
* What error is in the following code, how would you fix it:
* dim n as sting
* console.writeline("enter your name")
* n = console.readline
* Answer:
* The first line has a compilation (syntax) error. dim n as sting should read dim n as string
* What error is in the following code, how would you fix it:
* Dim names As String = {"Harry", "Dave", "Princess", "Nicky"}
* 'print all the names
* For x = 1 to 3
* Console.Writeline("name " & x & " = " & names(x))
* Next
* Answer:
* The third line has a Logic (semantic) error. For x = 1 to 3 should read For x = 0 to 3
* What error is in the following code, how would you fix it:
* Dim names As Sting = {"Harry", "Dave", "Princess", "Nicky"}
* Dim y As Integer
* y = Console.Readline
* 'print some of the names
* For x = 0 to y
* Console.Writeline("name " & x & " = " & names(x))
* Next
* Answer:
* Line 1 has a compilation error, Sting should read String. There is also a potential Runtime error on line 5, if the user inputs a value of y that is greater than 3 then the code will break. Errors like these can be solved in a number of ways, we are going to look at one now.

## Catching errors

For the above code we can easily break it if we type the following:
How old are you? cabbages!
The reason, as you should already be aware, is that variable age is an integer and you are trying to save the string cabbages into an integer. It's like trying to fit a cannon into a camel, they just aren't compatible, and VB will definitely complain ruining all your code. What is needed is a way in which we can stop or catch these errors, we are going to take a look at try and catch.
This will deal with the issue:
How old are you? Socrates! Conversion from string "Socrates!" to type 'Integer' is not valid. What is your name?
Letting us know that you're put a string in when it was expecting an integer. The program doesn't crash.
* Exercise: Validation
* Use a try and catch to avoid the issue of a person inputting a value for y that would break the array.
* Dim names As String = {"Harry", "Dave", "Princess", "Nicky"}
* Dim y As Integer
* y = Console.Readline
* 'print some of the names
* For x = 0 to y
* Console.Writeline("name " & x & " = " & names(x))
* Next
* Answer:
* Dim names As String = {"Harry", "Dave", "Princess", "Nicky"}
* Dim y As Integer
* y = Console.Readline
* 'print some of the names
* Try
* For x = 0 to y
* Console.Writeline("name " & x & " = " & names(x))
* Next
* Catch ex As Exception
* console.writeline("Looks like we're exceeded our array index")
* console.writeline(ex.message)
* End Try