tailieunhanh - C++ Programming for Games Module II phần 2

Tham khảo tài liệu 'c++ programming for games module ii phần 2', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Introduction Throughout most of the previous chapters we have assumed that all of our code was designed and implemented correctly and that the results could be anticipated. For example we assumed that the user entered the expected kind of input such as a string when a string was expected or a number when a number was expected. Additionally we assumed that the arguments we passed into function parameters were valid. But this may not always be true. For example what happens if we pass in a negative integer into a factorial function which expects an integer greater than or equal to zero Whenever we allocated memory we assumed that the memory allocation succeeded but this is not always true because memory is finite and can run out. While we copied strings with strcpy we assumed the destination string receiving the copy had enough characters to store a copy but what would happen if it did not It would be desirable if everything worked according to plan however in reality things tend to obey Murphy s Law which paraphrased says if anything can go wrong it will . In this chapter we spend some time getting familiar with several ways in which we can catch and handle errors. The overall goal is to write code that is easy to debug can possibly recover from errors and exits gracefully with useful error information if the program encounters a fatal error. Chapter Objectives Understand the method of catching errors via function return codes and an understanding of the shortcomings of this method. Become familiar with the concepts of exception handling its syntax and its benefits. Learn how to write assumption verification code using asserts. Error Codes The method of using error codes is simple. For every function or method we write we have it return a value which signifies whether the function method executed successfully or not. If it succeeded then we return a code that signifies success. If it failed then we return a predefined value that specifies where and why the .