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: Let's take a look at what each of them mean.
 * Compilation Errors
 * Run-time Errors
 * Logic Errors

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. 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  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:

Name and give examples of the three error types in programming code:


 * Compilation (Syntax)
 * Logic (Symantic)
 * Runtime

What error is in the following code, how would you fix it:

There is a Runtime error. will never be greater than 5, since line 3 gives  the value of , 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:

The first line has a compilation (syntax) error. should read

What error is in the following code, how would you fix it:

The third line has a Logic (semantic) error. should read

What error is in the following code, how would you fix it:

Line 1 has a compilation error,  should read. 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  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.

Use a try and catch to avoid the issue of a person inputting a value for  that would break the array.



