Sie sind auf Seite 1von 62

Chapter 17

Exception Handling
1. Trần Thị Ánh Linh
2. Phạm Quang Minh
3. Võ Huỳnh Hữu Sang
4. TS. Nguyễn Tấn Trần Minh Khang

TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
1
17.1 EXCEPTION HANDLING
FUNDAMENTALS
TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
2
Exception Handling Fundamentals
─ C++ exception handling is built upon
three keywords: try, catch, and throw

─ A catch statement immediately follows


the try block.

─ The general
form of try and catch are shown here:

TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
3
Exception Handling Fundamentals
─ The try block must contain the portion of your program
that you want to monitor for errors.

─ When an exception is thrown, it is caught by its


corresponding catch statement, which then processes
the exception.

─ The general form of the throw statement is shown here:

throw exception;

TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
4
Exception Handling Fundamentals
─ Input: Catch an integer type exception.
1. try
2. { // start a try block
3. cout << "Inside try block\n";
4. throw 99; // throw an error
5. cout << "This will not execute";
6. }
7. catch (int i)
8. { // catch an error
9. cout << "Caught an exception -- value is: ";
10. cout << i << "\n";
11.}
TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
5
Exception Handling Fundamentals
─ Example: throws an exception which is not caught by catch
and the program will be terminated abnormally.
1. try
2. { // start a try block
3. cout << "Inside try block\n";
4. throw 99; // throw an error
5. cout << "This will not execute";
6. }
7. catch (double i)
8. { // catch an error
9. cout << "Caught an exception -- value is: ";
10. cout << i << "\n";
11.}
TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
6
Exception Handling Fundamentals
An exception thrown by a function called from
within a try block can be caught by that try block
Example:
1. void Xtest(int test)
2. {
3. cout << "Inside Xtest, test is: “;
4. cout << test << "\n";
5. if(test)
6. throw test;
7. }
TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
7
Exception Handling Fundamentals
8. try
9. { // start a try block
10. cout << "Inside try block\n";
11. Xtest(0);
12. Xtest(1);
13. Xtest(2);
14.}
15.catch (int i)
16.{ // catch an error
17. cout << "Caught an exception -- value is: ";
18. cout << i << "\n";
19.}

TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
8
Exception Handling Fundamentals
─ A try block can be localized to a function.
1. // A try/catch is reset each time a function is
entered.
2. void Xhandler(int test)
3. {
4. try
5. {
6. if(test)
7. throw test;
8. }
9. catch(int i)
10. {
11. cout << "Caught One! Ex. #: " << i << '\n';
12. }
13.} TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH
[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
9
Exception Handling Fundamentals
─ A try block can be localized to a function.
14.int main()
15.{
16. Xhandler(1);
17. Xhandler(2);
18. Xhandler(0);
19. Xhandler(3);
20. return 0;
21.}

TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
10
Exit( ) and Abort( )
─ The exit( ) and abort( ) functions are cause the
termination of a program, but in different ways.

─ The exit( ) function causes the immediate, orderly


termination of a program

─ It has the following prototype:


void exit(int status);

TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
11
Exit( ) and Abort( )
─ The abort( ) function has this prototype:
void abort( );

─ The abort( ) function is a C++ program’s


“emergency stop” function.

─ Both exit( ) function and abort( ) function


requires the header <ctsdlib>

TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
12
Catching Class Types
─ An exception can be of any type, including class types
that you create.

─ Defining a class type for an exception is to create an


object that describes the error that occured.

─ This information can be used by the exception handler to


help it process the error.

TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
13
Catching Class Types
1.class MyException
2.{
3. public:
4. char str_what[80];
5. MyException()
6. {
7. *str_what = 0;
8. }
9. MyException(char *s)
10. {
11. strcpy_s(str_what, s);
12. }
13.};

TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
14
Catching Class Types
1. int main()
2. {
3. int a;
4. int b;
5. try
6. {
7. cout << "Enter numerator and denominator: ";
8. cin >> a >> b;
9. if (!b)
10. throw MyException("Cannot divide by zero!“);
11. else
12. cout << "Quotient is " << a / b << "\n";
13. }
14. catch (MyException e)
15. { // catch an error
16. cout << e.str_what << "\n";
17. }
18.} TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH
[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
15
Using Multiple Catch
Statements
─ You can associate more than one catch statement with a try.
However, each catch statement responds only to its own
type.

─ Catch expressions are checked in the order in which they


occur in a program.

─ Only a matching statement is executed. All other catch


blocks are ignored.

TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
16
Using Multiple Catch
Statements
1. void Xhandler(int test)
2. {
3. try
4. {
5. if(test)
6. throw test;
7. else
8. throw "Value is zero“;
9. }
10. catch(int i)
11. {
12. cout << "Caught One! Ex. #: " << i << ‘\n’;
13. }
14. catch(char *str)
15. {
16. cout << "Caught a string: “;
17. cout << str << '\n’;
18. }
19.}
TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
17
Using Multiple Catch
Statements
1. int main()
2. {
3. Xhandler(1);
4. Xhandler(2);
5. Xhandler(0);
6. Xhandler(3);
7. return 0;
8. }

TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
18
Catching Base Class
Exceptions
─ A catch clause for a base class will also match any class
derived from that base.
─ Example:
1.class B
2.{
3.};
4.class D : public B
5.{
6.};

TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
19
Catching Base Class
Exceptions
14. catch (B b)
7. int main()
8. { 15. {
16. cout << "Caught a base
9. D derived;
class.\n“;
10. try 17. }
11. { 18. catch (D d)
12. throw derived; 19. {
13. } 20. cout << "This won’t
execute.\n“;
21. }
22. return 0;
23.}
TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
20
7.3 OPTIONS FOR EXCEPTION
HANDLING
TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
21
Catching All Exceptions
─ You will want an exception handler to catch all
exceptions, instead of just a certain type.

─ Simply use this form of catch:


catch(...)
{
// process all exceptions
}
─ Here, the ellipsis matches any type of data.

TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
22
Catching All Exceptions
─ The following program illustrates catch(...) :
1. void Xhandler(int test)
2. {
3. try
4. {
5. if (test == 0)
6. throw test; // throw int
7. if (test == 1)
8. throw 'a’; // throw char
9. if (test == 2)
10. throw 123.23; // throw double
11. }
12. catch (...)
13. { // catch all exceptions
14. cout << "Caught One!\n“;
15. TRƯỜNG
} ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH
23
16.} [T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
Catching All Exceptions
17.int main()
18.{
19. Xhandler(0);
20. Xhandler(1);
21. Xhandler(2);
22. return 0;
23.}

TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
24
Catching All Exceptions
─ One very good use for catch(...) is as the last catch of a
cluster of catches.

─ In this capacity, it provides a useful default or “catch all”


statement

─ Using catch(...) as a default is a good way to catch all


exceptions that you don’t want to handle explicitly

TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
25
Catching All Exceptions
─ Input: For example, this program catches integer
exception but relies upon catch(...) to catch all other
1. void Xhandler(int test)
2. {
3. try
4. {
5. if (test == 0)
6. throw test; // throw int
7. if (test == 1)
8. throw 'a’; // throw char
9. if (test == 2)
10. throw 123.23;// throw double
11. }

TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
26
Catching All Exceptions
12. catch (int i)
13. { // catch an int exception
14. cout << "Caught " << i << ‘\n’;
15. }
16. catch (...)
17. { // catch all other exceptions
18. cout << "Caught One!\n”;
19. }
20.}

TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
27
Catching All Exceptions
21.int main()
22.{
23. Xhandler(0);
24. Xhandler(1);
25. Xhandler(2);
26. return 0;
27.}

TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
28
Restricting Exceptions
Thrown by a Function
─ You can restrict the type of exceptions that a function
can throw outside of itself.

─ To accomplish these restrictions, you must add a throw


clause to a function definition.

─ The general form of this clause is


ret-type func-name(arg-list) throw(type-
list)
{
// ...
}
TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
29
Restricting Exceptions
Thrown by a Function
Example: Restrict exception ( int, char, double ).
1. void Xhandler(int test) throw(int, char, double)
2. {
3. if (test == 0)
4. throw test; // throw int
5. if (test == 1)
6. throw 'a’; // throw char
7. if (test == 2)
8. throw 123.23; // throw double
9. }
TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
30
Restricting Exceptions
Thrown by a Function
16. catch (int i)
10.int main() 17. {
11.{ 18. cout << "Caught int“;
19. }
12. try
20. catch (char c)
13. { 21. {
14. Xhandler(0); 22. cout << "Caught char“;
15. } 23. }
24. catch (double d)
25. {
26. cout << "Caught double“;
27. }
28. return 0;
29.}
TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
31
Restricting Exceptions
Thrown by a Function
─ The following change to Xhandler( ) prevents it from throwing
any exceptions:
1. // This function can throw NO exceptions!
2. void Xhandler(int test) throw()
3. {
4. /* The following statements no longer work. Instead,
they will cause an abnormal program termination. */
5. if (test == 0)
6. throw test;
7. if (test == 1)
8. throw 'a’;
9. if (test == 2)
10. throw 123.23;
11.}TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH 32
[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
Rethrowing an Exception
─ You can rethrow an exception by calling throw by itself,
with no exception.

throw;

─ That exception will not be recaught by the same catch


statement but it’ll propagate to the immediately enclosing
try/catch sequence.

TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
33
Rethrowing an Exception
─ Input: The following program illustrates rethrowing an
exception. It rethrows a char * exception.
1. void Xhandler()
2. {
3. try
4. {
5. throw "hello“; // throw a char *
6. }
7. catch (char *) // catch a char *
8. {
9. cout << "Caught char * inside Xhandler\n“;
10. throw; // rethrow char * out of function
11. }
TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH
12.} [T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
34
Rethrowing an Exception
13.int main()
14.{
15. try
16. {
17. Xhandler();
18. }
19. catch (char *)
20. {
21. cout << "Caught char * inside main\n“;
22. }
23. return 0;
24.}
TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
35
17.3 HANDLING EXCEPTIONS
THROWN BY NEW
TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
36
Handling Exceptions Thrown
by new
─ In Standard C++, when an allocation request cannot
be honored, new throws a bad_alloc exception.

─ If you don’t catch this exception, then your program


will be terminated.

─ To have access to this exception, you must include


the header <new> in your program.

TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
37
Handling Exceptions Thrown
1.int main()
by new
2.{
3. int *p;
4. int i;
5. try
6. {// allocate memory for 32-
element int array
7. p = new int[32];
8. }
TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
38
Handling Exceptions Thrown
9.
by new
catch (bad_alloc xa)
10. {
11. cout << "Allocation failure.\n“;
12. return 1;
13. }
14. for (i = 0; i<32; i++)
15. p[i] = i;
16. for (i = 0; i<32; i++)
17. cout << p[i] << " ";
18. delete[] p; // free the memory
19. return 0;
20.}TRƯỜNG[T]ĐẠI028HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
39
The nothrow Alternative
─ In Standard C++, it is also possible to have new return
null, instead of having it throw an exception when an
allocation failure occurs .

─ This form of new is shown here:

p_var = new(nothrow) type;

TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
40
The nothrow Alternative
1. int main()
2. {
3. int *p;
4. int i;
5. p = new(nothrow) int[32]; // use nothrow option
6. if (!p)
7. {
8. cout << "Allocation failure.\n";
9. return 1;
10. }
11. for (i = 0; i<32; i++)
12. p[i] = i;
13. for (i = 0; i<32; i++)
14. cout << p[i] << " ";
15. delete[] p; // free the memory
16. return 0;
17.}
TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
41
17.4 OVERLOADING NEW AND
DELETE
TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
42
Overloading new and delete
─ The skeletons for the functions that overload new and
delete are shown here:

1.// Allocate an object.


2.void *operator new(size_t size)
3.{
4. /* Perform allocation. Throw
bad_alloc on failure.Constructor
called automatically. */
5. return pointer_to_memory;
6.}
TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
43
Overloading new and delete
─ The skeletons for the functions that overload new and
delete are shown here:

1. // Delete an object.
2. void operator delete(void *p)
3. {
4. /* Free memory pointed to by p.
5. Destructor called automatically.
*/
6. }

TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
44
Overloading new and delete
─ To allocate and free arrays of objects, you must use
these forms of new and delete:
1. // Allocate an array of objects.
2. void *operator new[](size_t size)
3. {
4. /* Perform allocation. Throw
bad_alloc exception on failure.
Each constructor called
automatically. */
5. return pointer_to_memory;
6. }
TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH
45
[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
Overloading new and delete
─ To allocate and free arrays of objects, you must use
these forms of new and delete:
1. // Delete an array of objects.
2. void operator delete[](void *p)
3. {
4. /* Free memory pointed to by p.
Destructor for each element
automatically called. */
5. }

TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
46
Overloading new and delete
─ The new and delete operators are generally overloaded
relative to a class.

─ To overload the new and delete operators relative to a


class, simply make the overloaded operator functions
class members

─ Here is an example:

TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
47
Overloading new and delete
1. class three_d
2. {
3. // 3-D coordinates
4. int x;
5. int y;
6. int z;
7. public:
8. three_d()
9. {
10. x = y = z = 0;
11. cout << "Constructing 0, 0, 0\n";
12. }
13. three_d(int i, int j, int k)
14. {
15. x = i; y = j; z = k;
16. cout << "Constructing " << i << ", ";
17. cout << j << ", " << k;
18. cout << '\n’;
19. TRƯỜNG
} ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH
[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
48
Overloading new and delete
19. ~three_d()
20. {
21. cout << "Destructing\n";
22. }
23. void *operator new(size_t size);
24. void *operator new[](size_t size);
25. void operator delete(void *p);
26. void operator delete[](void *p);
27. void show();
28.};

TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
49
Overloading new and delete
29.// new overloaded relative to three_d.
30.void *three_d::operator new(size_t size)
31.{
32. void *p;
33. cout << "Allocating three_d object.\n";
34. p = malloc(size);
35. // throw an exception on failure
36. if (!p)
37. {
38. bad_alloc ba;
39. throw ba;
40. }
41. return p;
42.}
TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
50
Overloading new and delete
43.// new overloaded relative to arrays of three_d.
44.void *three_d::operator new[](size_t size)
45.{
46. void *p;
47. cout << "Allocating array of three_d objects.\n";
48. // throw an exception on failure
49. p = malloc(size);
50. if (!p)
51. {
52. bad_alloc ba;
53. throw ba;
54. }
55. return p;
56.}

TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
51
Overloading new and delete
57.// delete overloaded relative to three_d.
58.void three_d::operator delete(void *p)
59.{
60. cout << "Deleting three_d object.\n";
61. free(p);
62.}
63.// delete overloaded relative to arrays of
three_d.
64.void three_d::operator delete[](void *p)
65.{
66. cout << "Deleting array of three_d objects.\n";
67. free(p);
68.}
TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
52
Overloading new and delete
69.// Show X, Y, Z coordinates.
70.void three_d::show()
71.{
72. cout << x << ", ";
73. cout << y << ", ";
74. cout << z << "\n";
75.}

TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
53
Overloading new and delete
76.int main()
77.{
78. three_d *p1, *p2;
79. try
80. {
81. p1 = new three_d[3]; // allocate array
82. p2 = new three_d(5, 6,7);// allocate
object
83. }
84. catch (bad_alloc ba)
85. {
86. cout << "Allocation error.\n";
87. return 1;
88. }

TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
54
Overloading new and delete
89. p1[1].show();
90. p2->show();
91. delete[] p1; // delete array
92. delete p2; // delete object
93. return 0;
94.}

TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
55
Overloading new and delete

TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
56
Overloading new and delete
─ When new and delete are overloaded relative to a
specific class, the use of these operators on any other
type of data causes the original new or delete to be
employed.

─ This means that if you add the following line to main( ),


the default new will be executed:

int *f = new int; //uses default new

─ It is also possible to overload new and delete globally.


TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
57
Overloading the nothrow
Version of new
─ You can also create overloaded nothrow versions of
new and delete. To do so, use these skeletons:
1. // Nothrow version of new.
2. void *operator new(size_t size, const
nothrow_t &n)
3. {
4. // Perform allocation.
5. if (success)
6. return pointer_to_memory;
7. else
8. return 0;
9. }
TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
58
Overloading the nothrow
Version of new
─ You can also create overloaded nothrow versions of
new and delete. To do so, use these skeletons:
1. // Nothrow version of new for arrays.
2. void *operator new[](size_t size, const
nothrow_t &n)
3. {
4. // Perform allocation.
5. if (success)
6. return pointer_to_memory;
7. else
8. return 0;
9. }
TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
59
Overloading the nothrow
Version of new
─ You can also create overloaded nothrow versions of
new and delete. To do so, use these skeletons:

// Nothrow version of delete.


void operator delete(void *p,
const nothrow_t &n)
{
// free memory
}

TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
60
Overloading the nothrow
Version of new
─ You can also create overloaded nothrow versions of
new and delete. To do so, use these skeletons:

// Nothrow version of delete for


arrays.
void operator delete[](void *p, const
nothrow_t &n)
{
// free memory
}

TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
61
TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN, KHU PHỐ 6, PHƯỜNG LINH TRUNG, QUẬN THỦ ĐỨC, TP. HỒ CHÍ MINH

[T] 028 3725 2002 101 | [F] 028 3725 2148 | [W] www.uit.edu.vn | [E] info@uit.edu.vn
62

Das könnte Ihnen auch gefallen