cpp:exception
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionNext revisionBoth sides next revision | ||
cpp:exception [2018/05/18 11:45] – created gthanos | cpp:exception [2018/05/18 12:33] – gthanos | ||
---|---|---|---|
Line 4: | Line 4: | ||
<code cpp Vector.cpp> | <code cpp Vector.cpp> | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | using namespace std; | ||
+ | |||
class Vector { | class Vector { | ||
int *array; | int *array; | ||
- | | + | int size; |
| | ||
public: | public: | ||
- | Vector(unsigned | + | Vector(int length=0); |
~Vector(); | ~Vector(); | ||
- | int & | + | int & |
}; | }; | ||
- | Vector:: | + | Vector:: |
size = length; | size = length; | ||
array = new (nothrow) int[size]; | array = new (nothrow) int[size]; | ||
Line 29: | Line 34: | ||
} | } | ||
- | int & | + | int & |
if(pos> | if(pos> | ||
cerr << " | cerr << " | ||
Line 41: | Line 46: | ||
<code cpp VectorUse.cpp> | <code cpp VectorUse.cpp> | ||
+ | #include " | ||
+ | |||
int main() { | int main() { | ||
int size; | int size; | ||
Line 54: | Line 61: | ||
<code cpp Vector.cpp> | <code cpp Vector.cpp> | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | using namespace std; | ||
+ | |||
class Vector { | class Vector { | ||
int *array; | int *array; | ||
- | | + | int size; |
| | ||
public: | public: | ||
- | Vector(unsigned | + | Vector(int length=0); |
~Vector(); | ~Vector(); | ||
- | int & | + | int & |
}; | }; | ||
- | Vector:: | + | Vector:: |
size = length; | size = length; | ||
array = new (nothrow) int[size]; | array = new (nothrow) int[size]; | ||
Line 75: | Line 87: | ||
} | } | ||
- | int & | + | int & |
- | if(pos>=length()) { | + | if(pos>=size) { |
cerr << " | cerr << " | ||
| | ||
Line 85: | Line 97: | ||
<code cpp VectorUse.cpp> | <code cpp VectorUse.cpp> | ||
+ | #include " | ||
+ | |||
int main() { | int main() { | ||
int size; | int size; | ||
Line 101: | Line 115: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | Αν και στο παραπάνω απλό παράδειγμα είναι προφανές ότι είναι πιο απλό να ελέγξει κανείς το μέγεθος της παραμέτρου size πριν καλέσει τον κατασκευαστή, | ||
+ | |||
+ | ===== Τύποι παραγόμενων εξαιρέσεων ===== | ||
+ | |||
+ | Στη C++ μπορείτε να δημιουργήσετε ένα Exception χρησιμοποιώντας οποιονδήποτε τύπο δεδομένων, | ||
+ | |||
+ | <code cpp> | ||
+ | throw -1; // throw an integer value | ||
+ | throw ENUM_INVALID_INDEX; | ||
+ | throw " | ||
+ | double pi=3.14159; throw pi; // throw a double variable that was previously defined | ||
+ | throw MyException(" | ||
+ | </ | ||
+ | |||
+ | |||
cpp/exception.txt · Last modified: 2023/05/15 14:01 by gthanos