Exception Handling
Mrs Ribeiro
Content
• Exception handling
• How to add exception handling
• Execution of a program with exception handling
• Exception handling - questions
Exception handling
• Sometimes we need to use a text box to accept numeric input
from the user (e.g. to accept a price)
• When the user enters non-numeric data (e.g. 4w) in the text box,
our program will give an execution/runtime error… because it
cannot convert the input to a numeric value. (4w cannot be
converted to an Integer by the CInt function).
Exception handling
Open the solution: SelectionSalaryVer1Complete
Exception handling
• Run the program with normal data. It gives the correct results.
• (hours 40, rate 100 will give Salary R4000 with 0 Overtime hours)
• (hours 42.5, rate 100 will give Salary R4500 with 2.5 Overtime hours)
Exception handling
• But when the user enters 100r for the rate, the program stops. (100r
cannot be converted to a numeric value). You will need to stop the
debugger. We must avoid this situation.
Exception handling
• Our program must display an error (instead of stopping) when the
hours or rate are not a valid numeric number … so that the user can
correct the input.
• We will do it using exception handling.
Exception handling
• We must add a try catch statement to the code in the button event
procedure (comments are not shown):
How to add exception handling
• To add a Try catch, do the following:
• Just before the first statement (in the button event procedure):
Type Try and press Enter
How to add exception handling
• a Try Catch statement is inserted in your code:
Sections of the Try Catch statement
• The Try block is used for all the code that must execute under
normal circumstances (if all input is fine):
Sections of the Try Catch statement
• The Catch block is used for all the code that must execute WHEN
AN EXCEPTION (non-numeric data) occurs.
• We will simply display an error message. The screen will be
displayed after the user acknowledged the error message. The
user will then have the ability to re-enter the data and click
“Calculate Salary” again.
How to add exception handling
• Select and move the button event code to the try block.
• Code after the move:
How to add exception handling
• Add code to the Catch block to display an error and put the
cursor on the first numeric text box on the screen.
Exception handling
• Run again the program. Enter invalid hours or rate or both.
• The program simply displays an error message to the user and
then the screen is displayed again. The user can now correct his
mistake and press “Calculate Salary” again.
Exception handling – Execution
• The red lines shows the execution of the code when valid data
(surname Mokoena, 40.5 hours, 100.50 rate) was entered by the user.
Exception handling – Execution
• The red lines shows the execution of the code when invalid data
(surname Mokoena, 40.5w hours, 100.50 rate) was entered by the user.
Exception handling – Execution
• The red lines shows the execution of the code when invalid data
(surname Mokoena, 40.5 hours, 100.50w rate) was entered by the user.
Exception handling – Question 1
• What would be displayed in the label if the user typed 5 in the text box
and pressed the button?
• Answer: (on the next page)
Exception handling – Question 1
• What would be displayed in the label if the user typed 5 in the text box
and pressed the button?
• Answer: 7
Exception handling – Question 2
• What would be displayed in the label if the user typed 5w in the text
box and pressed the button?
• Answer: (on the next page)
Exception handling – Question 2
• What would be displayed in the label if the user typed 5w in the text
box and pressed the button?
• Answer: 10
Exception handling – Question 3
• What would be displayed in the label if the user typed -10 in the text
box and pressed the button?
• Answer: (on the next page)
Exception handling – Question 3
• What would be displayed in the label if the user typed -10 in the text
box and pressed the button?
• Answer: 5
Exception handling – Question 4
• What would be displayed in the label if the user typed Sue in the text
box and pressed the button?
• Answer: (on the next page)
Exception handling – Question 4
• What would be displayed in the label if the user typed Sue in the text
box and pressed the button?
• Answer: 10
Summary
• Use a Try .. Catch statement so that the program can display an
error if the input is not numeric.
• The try block code STOPS immediately if some problem occurs
and control then go immediately to the Catch block to do the
error-handling (display the message and put the cursor on the
first numeric text box).
• If there are no problem, then the try block executes. The Catch
block is then skipped.
Summary
• You can also use a Text Box Leave Event to test for non numeric
data (to be discussed later)
• Any of the two methods (exception handling or the Text Box
Leave Event) may be used for validation.