tailieunhanh - A Complete Guide to Programming in C++ part 64
A Complete Guide to Programming in C++ part 64. This book provides both novice and experienced programmers with a comprehensive resource manual for the C++ programming language. Readers gain experience in all aspects of programming, from elementary language concepts to professional software development, with in depth coverage of all the language elements en route. These elements are carefully ordered to help the reader create useful programs every step of the way. | TRADITIONAL ERROR HANDLING 609 Error Conditions 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 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. Traditional Error Handling Traditional structured programming languages use normal syntax to handle errors errors in function calls are indicated by special return values global error variables or flags are set when errors occur and then checked again later. If a function uses its return value to indicate errors the return value must be examined whenever the function is called even if no error has occurred. Example if func 0 Return value positive . else Treat errors Error variables and flags must also be checked after every corresponding action. In other words you need to continually check for errors while a program is executing. If you do happen to forget to check for errors the consequences may be fatal. 610 CHAPTER 28 EXCEPTION HANDLING EXCEPTION HANDLING Using the throw statement Defining the function calc which throws exceptions. --------------------------------------------------------- class Error Infos about the error cause double calc int a int b if b 0 throw string Denominator is negative if b 0 Error errorObj throw errorObj return double a b EXCEPTION HANDLING 611 Exception Handling Concept C introduces a new approach to error handling. Exception handling is based on .
đang nạp các trang xem trước