1 / 7

Lezione XII

Laboratorio di Programmazione. Lezione XII. Per la gestione di operazioni che possono non andare a buon fine il C++ mette a disposizione un sistema di gestione delle eccezioni. Eccezioni. try { // codice da eseguire throw exception ; } catch ( type  exception )

Télécharger la présentation

Lezione XII

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Laboratorio di Programmazione Lezione XII

  2. Per la gestione di operazioni che possono non andare a buon fine il C++ mette a disposizione un sistema di gestione delle eccezioni. Eccezioni try { // codice da eseguire throw exception; } catch (type  exception) { // codice da eseguire in caso di eccezione }

  3. Il codice nel blocco try e’ eseguito normalmente. Se si verifica un errore, nel codice viene generata un’eccezione dalla parola chiave throw e un parametro. Il tipo del parametro specifica l’eccezione.Se nel blocco try e’ stato eseguito un throw, il blocco catch e’ eseguito ricevendo il parametro passato da throw. Eccezioni try { // codice da eseguire throw exception; } catch (type  exception) { // codice da eseguire in caso di eccezione }

  4. Il codice nel blocco try e’ eseguito normalmente. Se si verifica un errore, nel codice viene generata un’eccezione dalla parola chiave throw e un parametro. Il tipo del parametro specifica l’eccezione.Se nel blocco try e’ stato eseguito un throw, il blocco catch e’ eseguito ricevendo il parametro passato da throw. { char myarray[10]; try { for (int n=0; n<=10; n++) { if (n>9) throw “Accesso fuori limiti!"; myarray[n]='z'; } } catch (char * str) { cout << "Eccezione: " << str << endl; } return 0; } Eccezioni

  5. catch puo’ essere overloaded e quindi gestire diversi tipi di eccezioni. try { // throw ***; } catch (char * str) {} catch (int n) {} Inoltre catch puo’ gestire tutti i tipi di eccezioni se definito: catch(...) { // } Eccezioni

  6. La classe <exception> del C++ contiene un set di eccezioni standard. (bad_alloc, lanciata da new , ...) #include <iostream.h> #include <exception> int main () { try { // programma} catch (std::exception& e) { cout << "Exception: " << e.what(); } return 0; } Eccezioni

  7. Aggiungere la gestione delle eccezioni alla classe razionali • Aggiungere la gestione delle eccezioni alla classe pila • 3) Riscrivere la classe pila, sfruttando le eccezioni per controllare una allocazione dinamica della memoria (piu’ controlli standard su new...) Esercizi

More Related