Fundamentals of Programming: Exception Handling

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

PAPER 1 - ⇑ Fundamentals of programming ⇑

← Constants Exception handling Built-in functions →


Error types[edit | edit source]

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

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

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

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:

   Code Output
Price = 45.99
VAT = 9.198
Total = 55.188

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

   Code Output
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:

Console.Writeline("Total = " & Price + (Price * Tax))
Exercise: Exception Handling

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

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:

   Code Output

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:

   Code Output

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: Exception Handling
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


UNIT 1 - ⇑ Fundamentals of programming ⇑

← File handling Exception handling Debugging →