C/C++ Programming Techniques
ET2031/ ET2031E
Lecturer: PhD. DO Thi Ngoc Diep
SCHOOL OF ELECTRICAL AND ELECTRONIC ENGINEERING
HANOI UNIVERSITY OF SCIENCE AND TECHNOLOGY
Chapter 8: Exceptions
3
Content
Introduction
Trapping and catching mechanism
Exception handling in C++
4
Introduction
An exception is an event that occurs during program execution
disrupting the normal flow of the program
Exception is a special error
Due to wrong programming
Due to incorrect data input, due to system status (overflow)
etc.
When an exception occurs, if not handled, the program
immediately terminates and returns control to the operating
system.
Allocated resources are not released resource wasting
Without closing the file file cannot be accessed/corrupted
5
Introduction
Traditional error handling
Write the handling code where the error occurs
Make the program messed up, There is not always enough information to process,
It is not necessary to process
Transmit the state to the upper level
Via parameter, return value or global variable, Easy to mistake, Still confusing
Defect: Difficult to control all cases
Arithmetic errors, memory errors, Programmers often forget to handle errors,
Human nature: Inexperienced, deliberately omitted
In C: there are no preventive measures as well as simplified error
handling completely dependent on the programmer
Error handling in C++: introduced a "trap and catch" mechanism
(try .. catch .. throw) to enhance the ability as well as simplify error
handling