By: Lakshmi in C++ Tutorials on 2007年09月17日 [フレーム]
In C++, an exception is an object that is passed from the area of code where a problem occurs to the part of the code that is going to handle the problem. The type of the exception determines which area of code will handle the problem, and the contents of the object thrown, if any, may be used to provide feedback to the user.
The basic idea behind exceptions is fairly straightforward:
The basic steps in using exceptions are
When an exception is thrown (or raised), control transfers to the catch block immediately following the current try block.
NOTE:Some older compilers do not support exceptions. Exceptions are, however, part of the emerging C++ standard. All major compiler vendors have committed to supporting exceptions in their next releases, if they have not already done so. If you have an older compiler, you won't be able to compile and run the exercises in this chapter. It's still a good idea to read through the entire chapter, however, and return to this material when you upgrade your compiler.
Raising an exception.
0: #include <iostream.h>
1:
2: const int DefaultSize = 10;
3:
4: class Array
5: {
6: public:
7: // constructors
8: Array(int itsSize = DefaultSize);
9: Array(const Array &rhs);
10: ~Array() { delete [] pType;}
11:
12: // operators
13: Array& operator=(const Array&);
14: int& operator[](int offSet);
15: const int& operator[](int offSet) const;
16:
17: // accessors
18: int GetitsSize() const { return itsSize; }
19:
20: // friend function
21: friend ostream& operator<< (ostream&, const Array&);
22:
23: class xBoundary {}; // define the exception class
24: private:
25: int *pType;
26: int itsSize;
27: };
28:
29:
30: Array::Array(int size):
31: itsSize(size)
32: {
33: pType = new int[size];
34: for (int i = 0; i<size; i++)
35: pType[i] = 0;
36: }
37:
38:
39: Array& Array::operator=(const Array &rhs)
40: {
41: if (this == &rhs)
42: return *this;
43: delete [] pType;
44: itsSize = rhs.GetitsSize();
45: pType = new int[itsSize];
46: for (int i = 0; i<itsSize; i++)
47: pType[i] = rhs[i];
48: return *this;
49: }
50:
51: Array::Array(const Array &rhs)
52: {
53: itsSize = rhs.GetitsSize();
54: pType = new int[itsSize];
55: for (int i = 0; i<itsSize; i++)
56: pType[i] = rhs[i];
57: }
58:
59:
60: int& Array::operator[](int offSet)
61: {
62: int size = GetitsSize();
63: if (offSet >= 0 && offSet < GetitsSize())
64: return pType[offSet];
65: throw xBoundary();
66: return pType[0]; // appease MSC
67: }
68:
69:
70: const int& Array::operator[](int offSet) const
71: {
72: int mysize = GetitsSize();
73: if (offSet >= 0 && offSet < GetitsSize())
74: return pType[offSet];
75: throw xBoundary();
76: return pType[0]; // appease MSC
77: }
78:
79: ostream& operator<< (ostream& output, const Array& theArray)
80: {
81: for (int i = 0; i<theArray.GetitsSize(); i++)
82: output << "[" << i << "] " << theArray[i] << endl;
83: return output;
84: }
85:
86: int main()
87: {
88: Array intArray(20);
89: try
90: {
91: for (int j = 0; j< 100; j++)
92: {
93: intArray[j] = j;
94: cout << "intArray[" << j << "] okay..." << endl;
95: }
96: }
97: catch (Array::xBoundary)
98: {
99: cout << "Unable to process your input!\n";
100: }
101: cout << "Done.\n";
102: return 0;
103: }
Output: intArray[0] okay...
intArray[1] okay...
intArray[2] okay...
intArray[3] okay...
intArray[4] okay...
intArray[5] okay...
intArray[6] okay...
intArray[7] okay...
intArray[8] okay...
intArray[9] okay...
intArray[10] okay...
intArray[11] okay...
intArray[12] okay...
intArray[13] okay...
intArray[14] okay...
intArray[15] okay...
intArray[16] okay...
intArray[17] okay...
intArray[18] okay...
intArray[19] okay...
Unable to process your input!
Done.
Analysis:On line 23, a new class
is contained within the declaration of the boundary.
This new class is not in any way distinguished as an exception class. It is just
a class like any other. This particular class is incredibly simple: It has no
data and no methods. Nonetheless, it is a valid class in every way.
In fact, it is incorrect to say it has no methods, because the compiler automatically assigns it a default constructor, destructor, copy constructor, and the copy operator (operator equals); so it actually has four class functions, but no data.
Note that declaring it from within Array serves only to couple the two classes together. "Advanced Inheritance," Array has no special access to xBoundary, nor does xBoundary have preferential access to the members of Array.
On lines 60-66 and 69-75, the offset operators are modified to examine the offset requested and, if it is out of range, to throw the xBoundary class as an exception. The parentheses are required to distinguish between this call to the xBoundary constructor and the use of an enumerated constant. Note that Microsoft requires that you provide a return statement to match the declaration (in this case, returning an integer reference), even though if an exception is thrown on line 65 the code will never reach line 66. This is a compiler bug, proving only that even Microsoft finds this stuff difficult and confusing!
On line 89, the keyword try begins a try block that ends on line 96. Within that try block, 100 integers are added to the array that was declared on line 88.
On line 97, the catch block to catch xBoundary exceptions is declared.
In the driver program on lines 86-103, a try block is created in which each member of the array is initialized. When j (line 91) is incremented to 20, the member at offset 20 is accessed. This causes the test on line 63 to fail, and operator[] raises an xBoundary exception on line 65.
Program control switches to the catch block on line 97, and the exception is caught or handled by the catch on the same line, which prints an error message. Program flow drops through to the end of the catch block on line 100.
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in C++ )
Calculate average using Two-Dimensional Array in C++
Calculating total based on the given quantity and price in C++
Compute the square root of the sum of the squares of an array in C++
Program to add two numbers in C++
Sorting an array of Strings in C++
Matrix using nested for loops in C++
Compiling and Linking Multiple Source Files in C++
Programming errors a compiler will detect in C++
Two-Dimensional Array Manipulation in C++
Using the Built-in Arithmetic Types in C++
Concatenated String Literals in C++
assert() example program in C++
Latest Articles (in C++)
Calculating total based on the given quantity and price in C++
Sorting an array of Strings in C++
Matrix using nested for loops in C++
Compute the square root of the sum of the squares of an array in C++
Calculate average using Two-Dimensional Array in C++
Two-Dimensional Array Manipulation in C++
Compiling and Linking Multiple Source Files in C++
Escape Sequences for Nonprintable Characters in C++
Using the Built-in Arithmetic Types in C++
Calculating total based on the given quantity and price in C++
Sorting an array of Strings in C++
Matrix using nested for loops in C++
Compute the square root of the sum of the squares of an array in C++
Calculate average using Two-Dimensional Array in C++
Two-Dimensional Array Manipulation in C++
Compiling and Linking Multiple Source Files in C++
Escape Sequences for Nonprintable Characters in C++
Using the Built-in Arithmetic Types in C++
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate