Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

ANSI/ISO C++ Professional Programmer''''s Handbook phần 6 pot
Nội dung xem thử
Mô tả chi tiết
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 not a trivial task. The need to query the dynamic type of
exception led to the introduction of RTTI into C++. The additional overhead of exception handling derives from the
RTTI data structures, the "scaffolding" code that is generated by the compiler, and other implementation-dependent
factors. Exceptions can be grouped into categories; the standard exception classes are a good example of this. In
recent years, a few loopholes in the exception handling mechanism have been fixed. The first was the addition of
exception specifications to functions' prototypes. The second was the introduction of a function try block, which
enables the program to handle an exception that is thrown during the execution of the initializer expressions in the
constructor's member initialization list or during the execution of the constructor's body.
Exception handling is a very powerful and flexible tool for handling runtime errors effectively. However, use it
judiciously.
Contents
ANSI/ISO C++ Professional Programmer's Handbook - Chapter 6 - Exception Handling
file:///D|/Cool Stuff/old/ftp/1/1/ch06/ch06.htm (17 von 18) [12.05.2000 14:46:11]
© Copyright 1999, Macmillan Computer Publishing. All rights reserved.
ANSI/ISO C++ Professional Programmer's Handbook - Chapter 6 - Exception Handling
file:///D|/Cool Stuff/old/ftp/1/1/ch06/ch06.htm (18 von 18) [12.05.2000 14:46:11]