IN204 - Software Engineering - Object Oriented
Transcription
IN204 - Software Engineering - Object Oriented
IN204 - Software Engineering - Object Oriented Programming Tutorial 4 - Exceptions - October 21, 2016 B. Monsuez, F. Vedrine, A. Cutean, V. Paun Preamble You can find a reference guide on the IN204 course web page http://wwwdfr.ensta.fr/Cours/index.php? usebdd=ensta&sigle=IN204&afficher=biblio Exercise 1 Exception study Create a project and execute the code below. #include<iostream> doubledivide(doublea,doubleb); voidtest_divide(); voidtest_divide() { doublei,j; for(;;){ std::cout<<"Lenumerateur(0pourarreter):"; std::cin>>i; if(i==0) break; std::cout<<"Ledenominateur:"; std::cin>>j; std::cout<<"Resultat:"<<divide(i,j)<<std::endl; } } doubledivide(doublea,doubleb) { try{ if(!b)throwb; } catch(doubleb){ std::cout<<"Nepeutpasdiviserparzero.\n"; returnb; } returna/b; } Check behavior for the division by zero. Question 2 The previous code corresponds to one of the two uses that satisfies the rules of exception management, explain which rule is satisfied. Now want it to satisfy the other of two rules, how can we modify the code to make it so? Note: to revive an exception in a catch block just do throw; without any parameters. Question 3.1 We are going to further refine and get it right this time. We want to define a class that derives from the exception class std::exception found in the library "Exception" . This class will be called division_by_zero and will include implementing a counter for determining the number of times that this class was caught by a catch block. Naturally the division_by_zero class will derive from the class std::exception . It is assumed that the method void catched() informs the object it was captured by a catch block. And that the method int getCount () provides the number of times the exception was caught by a catch block. Define the class in question. Question 3.2 Modify the previous code so that it is the exception represented by the division_by_zero class that is raised. Question 3.3 We wish to implement the following code: voidtest_succesive_division() { doublei; std::cout<<"Lenumerateur:"; std::cin>>i; try{ successive_division(i); } catch(division_by_zeroe){ e.catched(); std::cout<<"Divisionparzeroauniveau"<<e.getCount() <<std::endl; } } doublesuccessive_division(doublei) { doublej; std::cout<<"Ledenominateursuivant(-1pourarreter):"; std::cin>>j; if(j==-1) returni; try{ successive_division(j); returndivide(i,j); } catch(division_by_zeroe){ e.catched(); throwe; } } Exercise 2 - Ensure that a certain code is always executed, either if an exception occurs or not In Java, you have the ability to write a finally block try { … } finally { … } The code placed in the finally block is always executed, if an exception occurs or if no exception occurs. Question 1.1 In C++, this construction does not exist. Is is possible to ensure the same thing in C++ using catch blocks? Note: The instruction try { … } catch(type1e) { //Capturelesexceptionsdérivantdetype1 } catch(type1e) { //Capturelesexceptionsdérivantdetype2siellesn’ontpasétécapturéesparles //exceptionsdetype1. } catch(…) { //Capturetouteslesexceptionsn’ayantpasétécapturéesparuneclauseprécédente } Question 1.2 We want the divide function defined in the previous question shows "function complete" at the end of the function, and even if an exception was thrown. How to deal with the solution proposed to the previous question? What is the disadvantage of this approach? 2.1 Question We suggest to circumvent the difficulty by using an object. We know that an object allocated on the stack is systematically destroyed when exits its activation block. When an object is destroyed, its destructor is called. Note: In the destructor, it is possible to execute the code that would have been placed in the finally block of JAVA that we wish to see systematically executed. Complete the following class to display "function complete" when the object is destroyed. classfinally { public: finally(){} ~finally(){…} … } Question 2.2 Implement this solution instead of the proposed solution to Question 1.2. Check this solution is working properly. Exercise 3 - Ensure that all exceptions are captured Question 1 The exception division_by_zero may be raised by the divide function. Add this information to the divide function. Question 2 Compile your program. What is happening then? Review and correct your program based on the information present in the course.