cpp:exception
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
cpp:exception [2018/05/18 12:33] – gthanos | cpp:exception [2018/05/18 13:45] – [Κληρονομικότητα] gthanos | ||
---|---|---|---|
Line 116: | Line 116: | ||
</ | </ | ||
+ | <WRAP tip 80% center round> | ||
Αν και στο παραπάνω απλό παράδειγμα είναι προφανές ότι είναι πιο απλό να ελέγξει κανείς το μέγεθος της παραμέτρου size πριν καλέσει τον κατασκευαστή, | Αν και στο παραπάνω απλό παράδειγμα είναι προφανές ότι είναι πιο απλό να ελέγξει κανείς το μέγεθος της παραμέτρου size πριν καλέσει τον κατασκευαστή, | ||
+ | </ | ||
===== Τύποι παραγόμενων εξαιρέσεων ===== | ===== Τύποι παραγόμενων εξαιρέσεων ===== | ||
Line 128: | Line 129: | ||
double pi=3.14159; throw pi; // throw a double variable that was previously defined | double pi=3.14159; throw pi; // throw a double variable that was previously defined | ||
throw MyException(" | throw MyException(" | ||
+ | </ | ||
+ | |||
+ | ===== Δημιουργία και διαχείριση της εξαίρεσης ===== | ||
+ | |||
+ | Όπως σε όλες τις γλώσσες αντικειμενοστραφούς προγραμματισμού η παραγωγή μιας εξαίρεσης θα πρέπει να γίνει μέσα σε ένα //try block// και η διαχείριση της μέσα σε ένα //catch block// που ακολουθεί το //try block//. Δείτε το παρακάτω ενδεικτικό παράδειγμα. | ||
+ | |||
+ | <code cpp ExceptionHandling.cpp> | ||
+ | #include < | ||
+ | using namespace std; | ||
+ | |||
+ | class MyException: | ||
+ | public: | ||
+ | const char* what() const throw() { | ||
+ | return "Just another std:: | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | int main() { | ||
+ | try { | ||
+ | int option; | ||
+ | cout << "Enter option (1-5): "; | ||
+ | cin >> option; | ||
+ | short int c; | ||
+ | MyException ex; | ||
+ | switch(option) { | ||
+ | case 1: | ||
+ | throw 10; // throw an int literal | ||
+ | break; | ||
+ | case 2: | ||
+ | throw 2.5; //throw a double literal | ||
+ | break; | ||
+ | case 3: | ||
+ | throw " | ||
+ | break; | ||
+ | case 4: | ||
+ | throw string(" | ||
+ | break; | ||
+ | case 5: | ||
+ | throw ex; //throw a MyException object | ||
+ | break; | ||
+ | default: | ||
+ | c = -10; throw c; // throw a character (default option) | ||
+ | break; | ||
+ | } | ||
+ | } catch(int ex) { | ||
+ | cout << "Got '"<< | ||
+ | } catch(double ex) { | ||
+ | cout << "Got '"<< | ||
+ | } catch(const char *ex) { | ||
+ | cout << "Got char* '"<< | ||
+ | } catch(const string &ex) { | ||
+ | cout << "Got string '"<< | ||
+ | } catch(const MyException &ex) { | ||
+ | cout << "Got '"<< | ||
+ | } catch(...) { | ||
+ | cout << "Got an exception of unknown type!\n"; | ||
+ | } | ||
+ | cout << " | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | <WRAP tip 80% center round> | ||
+ | Στον παραπάνω κώδικα μπορείτε να παρατηρήσετε τα διαφορετικά μηνύματα που παράγονται ανάλογα με τον τύπο της εξαίρεσης. Παρατηρήστε επίσης ότι αν και παράγεται ένα αντικείμενο τύπου //short int//, το οποίο χωράει σε ένα //int// δεν γίνεται κάποια αυτόματη μετατροπή τύπου, ώστε το //catch block// που πιάνει τύπους //int// να πιάσει και αντικείμενα τύπου short int. | ||
+ | </ | ||
+ | |||
+ | ===== Κληρονομικότητα ===== | ||
+ | |||
+ | Ας υποθέσουμε ότι έχουμε τη σχέση κληρονομικότητας μεταξύ των κλάσεων **IDException** και **CountedIDException**, | ||
+ | |||
+ | <code cpp BaseException.h> | ||
+ | class BaseException: | ||
+ | protected: | ||
+ | int a; | ||
+ | public: | ||
+ | BaseException(int a) { this->a = a; } | ||
+ | const char* what() const throw() { | ||
+ | char s[64]; | ||
+ | sprintf(s, " | ||
+ | return s; | ||
+ | } | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | <code cpp DerivedException.h> | ||
+ | #include " | ||
+ | class DerivedException: | ||
+ | int b; | ||
+ | public: | ||
+ | DerivedException(int a, int b): Base(a) { this->b = b; } | ||
+ | const char* what() const throw() { | ||
+ | char s[64]; | ||
+ | sprintf(s, " | ||
+ | return s; | ||
+ | } | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | <code cpp ExceptionUse.cpp> | ||
+ | #include < | ||
+ | using namespace std; | ||
+ | |||
+ | int main() { | ||
+ | try { | ||
+ | } catch(BaseException ex) { | ||
+ | cout << ex.what(); | ||
+ | } catch(DerivedException ex) { | ||
+ | cout << ex.what(); | ||
+ | } | ||
+ | return 0; | ||
+ | } | ||
</ | </ | ||
cpp/exception.txt · Last modified: 2023/05/15 14:01 by gthanos