User Tools

Site Tools


cpp:exception

Differences

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

Link to this comparison view

Next revision
Previous revision
Next revisionBoth sides next revision
cpp:exception [2018/05/18 11:45] – created gthanoscpp:exception [2018/05/18 12:51] – [Τύποι παραγόμενων εξαιρέσεων] gthanos
Line 4: Line 4:
  
 <code cpp Vector.cpp> <code cpp Vector.cpp>
 +#include <iostream>
 +#include <cstdlib>
 +
 +using namespace std;
 +
 class Vector { class Vector {
   int *array;   int *array;
-  unsigned int size;+  int size;
      
 public: public:
-  Vector(unsigned int length=0);+  Vector(int length=0);
   ~Vector();   ~Vector();
-  int &valueAt(unsigned int pos) const;  // returns a reference to element at position pos+  int &valueAt(int pos) const;  // returns a reference to element at position pos
 }; };
  
-Vector::Vector(unsigned int length) {+Vector::Vector(int length) {
   size = length;   size = length;
   array = new (nothrow) int[size];   array = new (nothrow) int[size];
Line 29: Line 34:
 } }
  
-int &Vector::valueAt(unsigned int pos) const {+int &Vector::valueAt(int pos) const {
   if(pos>=length()) {   if(pos>=length()) {
      cerr << "Invalid access position!\n";      cerr << "Invalid access position!\n";
Line 41: Line 46:
  
 <code cpp VectorUse.cpp> <code cpp VectorUse.cpp>
 +#include "Vector.cpp"
 +
 int main() { int main() {
   int size;   int size;
Line 54: Line 61:
  
 <code cpp Vector.cpp> <code cpp Vector.cpp>
 +#include <iostream>
 +#include <cstdlib>
 +
 +using namespace std;
 +
 class Vector { class Vector {
   int *array;   int *array;
-  unsigned int size;+  int size;
      
 public: public:
-  Vector(unsigned int length=0);+  Vector(int length=0);
   ~Vector();   ~Vector();
-  int &valueAt(unsigned int pos) const;  // returns a reference to element at position pos+  int &valueAt(int pos) const;  // returns a reference to element at position pos
 }; };
  
-Vector::Vector(unsigned int length) {+Vector::Vector(int length) {
   size = length;   size = length;
   array = new (nothrow) int[size];   array = new (nothrow) int[size];
Line 75: Line 87:
 } }
  
-int &Vector::valueAt(unsigned int pos) const { +int &Vector::valueAt(int pos) const { 
-  if(pos>=length()) {+  if(pos>=size) {
      cerr << "Invalid access position!\n";      cerr << "Invalid access position!\n";
      return array[size-1];      return array[size-1];
Line 85: Line 97:
  
 <code cpp VectorUse.cpp> <code cpp VectorUse.cpp>
 +#include "Vector.cpp"
 +
 int main() { int main() {
   int size;   int size;
Line 101: Line 115:
 } }
 </code> </code>
 +
 +<WRAP tip 80% center round>
 +Αν και στο παραπάνω απλό παράδειγμα είναι προφανές ότι είναι πιο απλό να ελέγξει κανείς το μέγεθος της παραμέτρου size πριν καλέσει τον κατασκευαστή, κάτι τέτοιο είναι δύσκολο να εφαρμοστεί σε όλες τις περιπτώσεις, όπως για παράδειγμα το μέγεθος **size** παράγεται δυναμικά από το πρόγραμμα και ο κατασκευαστής καλείται σε αρκετά διαφορετικά σημεία του προγράμματος.
 +</WRAP>
 +===== Τύποι παραγόμενων εξαιρέσεων =====
 +
 +Στη C++ μπορείτε να δημιουργήσετε ένα Exception χρησιμοποιώντας οποιονδήποτε τύπο δεδομένων, δηλαδή δεν απαιτείται τα αντικείμενα που παράγονται να είναι απόγονοι συγκεκριμένης κλάσης. Δείτε μερικά παραδείγματα παραγωγής έγκυρων //exceptions// παρακάτω:
 +
 +<code cpp>
 +throw -1;                     // throw an integer value
 +throw ENUM_INVALID_INDEX;     // throw an enum value
 +throw "Invalid argument!";    // throw a literal C-style (const char*) string
 +double pi=3.14159; throw pi;  // throw a double variable that was previously defined
 +throw MyException("Fatal!");  // Throw an object of class MyException
 +</code>
 +
 +===== Δημιουργία και διαχείριση της εξαίρεσης =====
 +
 +Όπως σε όλες τις γλώσσες αντικειμενοστραφούς προγραμματισμού η παραγωγή μιας εξαίρεσης θα πρέπει να γίνει μέσα σε ένα //try block// και η διαχείριση της μέσα σε ένα //catch block// που ακολουθεί το //try block//. Δείτε το παρακάτω ενδεικτικό παράδειγμα.
 +
 +<code cpp ExceptionHandling.cpp>
 +#include <iostream>
 +using namespace std;
 +
 +class MyException: public std::exception {
 +  const char* what() {
 +    return "Just another std::exception";
 +  }
 +}
 +
 +int main() {
 +  try {
 +    throw 1;
 +    throw 2.5;
 +    throw "C++";
 +    MyException ex; throw ex;
 +  } catch(int ex) {
 +    cout << "Got '<< ex <<"'!\n";
 +  } catch(double ex) {
 +    cout << "Got '<< ex <<"'!\n";
 +  } catch(const char *ex) {
 +    cout << "Got '<< ex <<"'!\n";
 +  } catch(const string &ex) {
 +    cout << "Got '<< ex <<"'!\n";
 +  } catch(const MyException &ex) {
 +    cout << "Got '<< ex.what() <<"'!\n";
 +  } 
 +}
 +</code>
 +
 +
 +
  
cpp/exception.txt · Last modified: 2023/05/15 14:01 by gthanos