User Tools

Site Tools


cpp:exception

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
cpp:exception [2018/05/18 12:51] – [Τύποι παραγόμενων εξαιρέσεων] gthanoscpp:exception [2018/05/18 13:43] – [Δημιουργία και διαχείριση της εξαίρεσης] gthanos
Line 140: Line 140:
  
 class MyException: public std::exception { class MyException: public std::exception {
-  const char* what() {+public: 
 +  const char* what() const throw() {
     return "Just another std::exception";     return "Just another std::exception";
   }   }
-}+};
  
 int main() { int main() {
   try {   try {
-    throw 1; +    int option; 
-    throw 2.5; +    cout << "Enter option (1-5): "
-    throw "C++"; +    cin >> option; 
-    MyException ex; throw ex;+    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 "C++"; //throw a char * literal 
 +        break; 
 +      case 4: 
 +        throw string("C++"); //throw a 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) {   } catch(int ex) {
-    cout << "Got '<< ex <<"'!\n";+    cout << "Got '"<< ex <<"'!\n";
   } catch(double ex) {   } catch(double ex) {
-    cout << "Got '<< ex <<"'!\n";+    cout << "Got '"<< ex <<"'!\n";
   } catch(const char *ex) {   } catch(const char *ex) {
-    cout << "Got '<< ex <<"'!\n";+    cout << "Got char* '"<< ex <<"'!\n";
   } catch(const string &ex) {   } catch(const string &ex) {
-    cout << "Got '<< ex <<"'!\n";+    cout << "Got string '"<< ex <<"'!\n";
   } catch(const MyException &ex) {   } catch(const MyException &ex) {
-    cout << "Got '<< ex.what() <<"'!\n"; +    cout << "Got '"<< ex.what() <<"'!\n"; 
-  } +  } catch(...) { 
 +    cout << "Got an exception of unknown type!\n"; 
 +  } 
 +  cout << "Successfully handled the created exception!\n";
 } }
 </code> </code>
  
 +<WRAP tip 80% center round>
 +Στον παραπάνω κώδικα μπορείτε να παρατηρήσετε τα διαφορετικά μηνύματα που παράγονται ανάλογα με τον τύπο της εξαίρεσης. Παρατηρήστε επίσης ότι αν και παράγεται ένα αντικείμενο τύπου //short int//, το οποίο χωράει σε ένα //int// δεν γίνεται κάποια αυτόματη μετατροπή τύπου, ώστε το //catch block// που πιάνει τύπους //int// να πιάσει και αντικείμενα τύπου short int.
 +</WRAP>
 +
 +===== Κληρονομικότητα =====
 +
 +Ας υποθέσουμε ότι έχουμε τη σχέση κληρονομικότητας μεταξύ των κλάσεων **IDException** και **CountedIDException**, όπως παρακάτω:
 +
 +<code cpp BaseException.h>
 +class BaseException: public std::exception {
 +protected:
 +  int a;
 +public:
 +  BaseException(int a) { this->a = a; }
 +  const char* what() const throw() {
 +    char s[64];
 +    sprintf(s, "BaseException, a: %d", a);
 +    return s;
 +  }
 +};
 +</code>
 +
 +<code cpp CountedIDException.h>
 +#include "BaseException.h"
 +class DerivedException: public BaseException {
 +  int b;
 +public:
 +  DerivedException(int a, int b): Base(a) { this->b = b; }
 +  const char* what() const throw() {
 +    char s[64];
 +    sprintf(s, "DerivedException, a: %d, b: %d", a, b);
 +    return s;
 +  }
 +};
 +</code>
 +
 +<code cpp IDExceptionUse.cpp>
 +#include <iostream>
 +using namespace std;
 +
 +int main() {
 +  try {
 +  } catch(BaseException ex) {
 +    cout << ex.what();
 +  } catch(DerivedException ex) {
 +    cout << ex.what();
 +  }
 +  return 0;
 +}
 +</code>
  
  
  
cpp/exception.txt · Last modified: 2023/05/15 14:01 by gthanos