Fundamentals of Programming: Validation
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:
For x = 1 two 9 console.writeline(x) Next
You should be able to see that in line 1 the programmer has misspelt the word
to. This code won't work at all.
Sometimes you will have a program that compiles fine, but breaks when you actually run it. For example this code here:
Dim x as integer = 0 Dim total as integer = 0 While x < 5 total = total + 1 Loop
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:
Dim Price as decimal = 45.99
Dim Tax as decimal = 0.20
Console.Writeline("Price = " & Price)
Console.Writeline("VAT = " & Price * Tax)
Console.Writeline("Total = " & Price + Tax)
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:
Console.Writeline("Total = " & Price + (Price * Tax))
There is a Runtime error.
The first line has a compilation (syntax) error.
The third line has a Logic (semantic) error.
Line 1 has a compilation error,
Dim age as integer console.writeline("How old are you?") age = console.readline() console.writeline("What is your name?")
For the above code we can easily break it if we type the following:
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.
Dim age as integer console.writeline("How old are you?") Try age = console.readline() Catch ex As Exception console.writeline(ex.message) End Try console.writeline("What is your name?")
This will deal with the issue:
Letting us know that you're put a string in when it was expecting an integer. The program doesn't crash.