0% found this document useful (0 votes)
34 views14 pages

Computer Programming Lecture 6.0 & Practicals

c++ 5 pdf

Uploaded by

akidasimndwe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views14 pages

Computer Programming Lecture 6.0 & Practicals

c++ 5 pdf

Uploaded by

akidasimndwe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

COMPUTER PROGRAMMING

LECTURE 6.0 & PRACTICALS


OBJECTIVES
•Understanding errors.
•Introduction to exception handling.
•Writing better and stable programs.
INTRODUCTION
•Programming errors results from bad code and produces undesired/incorrect results.
•Programming errors all into three categories:
• Compilation Errors-errors that prevent your program from running.
• Runtime Errors-errors that occur while your program runs e.g. a program tries to carry out an impossible
operation.
• Logic Errors-errors that prevent your program from doing what you intend to do.
Errors
•Errors that occur at program runtime can seriously interrupt the normal flow of a program.
•Some common causes of errors are:
• division by 0, or values that are too large or small for a type
• no memory available for dynamic allocation
• errors on file access, for example, file not found
• attempt to access an invalid address in main memory
• invalid user input
Errors…
•Anomalies like these lead to incorrect results and may cause a computer to crash.
•Both of these cases can have fatal effects on your application.
•One of the programmer’s most important tasks is to predict and handle errors.
•You can judge a program’s quality by the way it uses error-handling techniques to counteract
any potential error, although this is by no means easy to achieve.
Errors …
•Consider the example below: •Suppose a user tries to enter a name or some
random letters as inputs.
•The computer is not prepared to multiply two
strings or one number and a string thus a
problem arises.
Errors and Exception Handling
•An exception is a situation that would be unusual for the program that is being processed.
•As a programmer, you should anticipate any abnormal behavior that could be caused by the user
entering wrong information that could otherwise lead to unpredictable results.
•An error result or an unpredictable behavior on your program not caused by the operating
system but that occurs in your program is called an exception.
•The ability to deal with a program’s eventual abnormal behavior is called exception handling. C+
+ provides three keywords to handle an exception as discussed on the next slide.
Errors and Exception Handling
•1. Trying the normal flow: To deal with the expected behavior of a program, use the try
keyword as in the following syntax:

try {Behavior}

•The try keyword is required.


•It lets the compiler know that you are anticipating an abnormal behavior and will try to deal
with it.
•The actual behavior that needs to be evaluated is included between an opening curly bracket “{“
and a closing curly bracket “}”.
•Inside of the brackets, implement the normal flow that the program should follow, at least for
this section of the code.
Errors and Exception Handling
• 2. Catching Errors: During the flow of the program as part of the try section, if an abnormal behavior
occurs, instead of letting the program crash or instead of letting the compiler send the error to the
operating system, you can transfer the flow of the program to another section that can deal with it.
• The syntax used by this section is:

• catch(Argument) {WhatToDo}
• This section always follows the try section and there must not be any code between the try’s closing
bracket and the catch section.
• The catch keyword is required and follows the try section.
• The catch behaves a little like a function.
• It uses an argument that is passed by the previous try section.
Errors and Exception Handling
•The argument can be a regular variable or a class.
•If there is no argument to pass, the catch must at least take a three-period argument as
in catch(…).
•The behavior of the catch clause starts with an opening curly bracket “{“ and ends with a closing
curly bracket “}”.
•The inside of the brackets is called the body of the catch clause.
•Therefore, use the body of the catch to deal with the error that was caused.
Errors and Exception Handling
•Combined with the try block, the syntax of an exception would be:
Errors and Exception Handling
•Throwing an error: There are two main ways an abnormal program behavior is transferred from
the try block to the catch clause.
•This transfer is actually carried by the throw keyword.
•Unlike the try and catch blocks, the throw keyword is independent of a formal syntax but still
follows some rules.
Errors and Exception Handling
Errors and Exception Handling

You might also like