cpp:exception
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | Next revisionBoth sides next revision | ||
cpp:exception [2018/05/18 12:33] – [Διαχείριση Εξαιρέσεων] gthanos | cpp:exception [2018/05/18 12:51] – [Τύποι παραγόμενων εξαιρέσεων] gthanos | ||
---|---|---|---|
Line 130: | Line 130: | ||
throw MyException(" | throw MyException(" | ||
</ | </ | ||
+ | |||
+ | ===== Δημιουργία και διαχείριση της εξαίρεσης ===== | ||
+ | |||
+ | Όπως σε όλες τις γλώσσες αντικειμενοστραφούς προγραμματισμού η παραγωγή μιας εξαίρεσης θα πρέπει να γίνει μέσα σε ένα //try block// και η διαχείριση της μέσα σε ένα //catch block// που ακολουθεί το //try block//. Δείτε το παρακάτω ενδεικτικό παράδειγμα. | ||
+ | |||
+ | <code cpp ExceptionHandling.cpp> | ||
+ | #include < | ||
+ | using namespace std; | ||
+ | |||
+ | class MyException: | ||
+ | const char* what() { | ||
+ | return "Just another std:: | ||
+ | } | ||
+ | } | ||
+ | |||
+ | int main() { | ||
+ | try { | ||
+ | throw 1; | ||
+ | throw 2.5; | ||
+ | throw " | ||
+ | MyException ex; throw ex; | ||
+ | } catch(int ex) { | ||
+ | cout << "Got '<< | ||
+ | } catch(double ex) { | ||
+ | cout << "Got '<< | ||
+ | } catch(const char *ex) { | ||
+ | cout << "Got '<< | ||
+ | } catch(const string &ex) { | ||
+ | cout << "Got '<< | ||
+ | } catch(const MyException &ex) { | ||
+ | cout << "Got '<< | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
cpp/exception.txt · Last modified: 2023/05/15 14:01 by gthanos