Fundamentals of Programming: Validation

From Wikibooks, open books for an open world
Jump to: navigation, search

UNIT 1 - ⇑ Fundamentals of Programming ⇑

← File handling Validation The Role of Variables →


Error types[edit]

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[edit]

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.

Run-time error[edit]

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[edit]

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:

  1. Dim Price as decimal = 45.99
    
  2. Dim Tax as decimal = 0.20
    
  3.  
    
  4. Console.Writeline("Price {{=}} " & Price)
    
  5. Console.Writeline("VAT {{=}} " & Price * Tax)
    
  6. Console.Writeline("Total {{=}} " & Price + Tax)
    

In the above example you would expect it to print out:

  Blank.svg Code Output
Down arrow Hexagonal Icon.svg

Price = 45.99
VAT = 9.198
Total = 55.188

But because there is a logic error on line 6, it prints:

  Blank.svg Code Output
Down arrow Hexagonal Icon.svg

Price = 45.99
VAT = 9.198
Total = 46.19

To fix it, you have to fix the logic of the code and change line 6 to:

  1. Console.Writeline("Total = " & Price + (Price * Tax))
    
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:

  1. dim x as integer
    
  2. do until x > 5
    
  3.   x = 1
    
  4.   x = x + 1
    
  5. 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:

  1. dim n as sting
    
  2. console.writeline("enter your name")
    
  3. 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:

  1. Dim names() As String = {"Harry", "Dave", "Princess", "Nicky"}
    
  2. 'print all the names
    
  3. For x = 1 to 3
    
  4.    Console.Writeline("name " & x & " = " & names(x))
    
  5. 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:

  1. Dim names() As Sting = {"Harry", "Dave", "Princess", "Nicky"}
    
  2. Dim y As Integer
    
  3. y = Console.Readline()
    
  4. 'print some of the names
    
  5. For x = 0 to y
    
  6.    Console.Writeline("name " & x & " = " & names(x))
    
  7. 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[edit]

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:

  Blank.svg Code Output
Down arrow Hexagonal Icon.svg

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.

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:

  Blank.svg Code Output
Down arrow Hexagonal Icon.svg

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.
  1. Dim names() As String = {"Harry", "Dave", "Princess", "Nicky"}
    
  2. Dim y As Integer
    
  3. y = Console.Readline()
    
  4. 'print some of the names
    
  5. For x = 0 to y
    
  6.    Console.Writeline("name " & x & " = " & names(x))
    
  7. Next
    

Answer :

  1. Dim names() As String = {"Harry", "Dave", "Princess", "Nicky"}
    
  2. Dim y As Integer
    
  3. y = Console.Readline()
    
  4. 'print some of the names
    
  5. Try
    
  6.    For x = 0 to y
    
  7.       Console.Writeline("name " & x & " = " & names(x))
    
  8.    Next
    
  9. Catch ex As Exception
    
  10.    console.writeline("Looks like we're exceeded our array index")
    
  11.    console.writeline(ex.message)
    
  12. End Try