tailieunhanh - ANSI/ISO C++ Professional Programmer's Handbook phần 6
Tham khảo tài liệu 'ansi/iso c++ professional programmer's handbook phần 6', 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ả | ANSI ISO C Professional Programmer s Handbook - Chapter 6 - Exception Handling while true infinitely cin num if num 99 throw Exit exit the loop cout you entered num enter another number endl catch Exit cout game over endl return 0 In the preceding example the programmer locates an infinite loop within a try block. The throw statement breaks the loop and transfers control to the following catch statement. This style of programming is not recommended however. It is very inefficient due to the excess overhead of exception handling. Furthermore it is rather verbose and might have been much simpler and shorter had it been written with a break statement. In demo apps such as this one the difference is mostly a stylistic one. In large-scale applications the use of exception handling as an alternative control structure imposes a significant performance overhead. Simple runtime errors that can be handled safely and effectively without the heavy machinery of exception handling need to also be treated by traditional methods. For example a password entry dialog box should not throw an exception if the user mistyped his or her password. It is much simpler to redisplay the password entry dialog again with an appropriate error message. On the other hand if the user enters wrong passwords dozens of times in a row this can indicate a malicious break-in attempt. In this case an exception should be thrown. The appropriate handler can page the system administrator and security officer. Conclusions The exception handling mechanism of C overcomes the problems associated with the traditional methods. It frees the programmer from writing tedious code that checks the success status of every function call. Exception handling also eliminates human mistakes. Another important advantage of exception handling is the automatic unwinding of the stack which ensures that local active objects are properly destroyed and their resources are released. Implementing an exception handling mechanism was .
đang nạp các trang xem trước