> donuts; cout << "Enter number of glasses of milk: "; cin >> milk; if (milk <= 0) { cout << donuts << " donuts and NO milk!\n" << "Go buy some milk.\n"; } else { dpg = donuts / (double) milk; cout << donuts << " donuts.\n" << milk << " glasses of milk.\n" << "You have " << dpg << " donuts for each glass of milk.\n"; } cout << “End of program.\n"; } Enter number of donuts: 12 Enter number of glasses of milk: 0 12 donuts and NO milk! Go buy some milk! End of program."> > donuts; cout << "Enter number of glasses of milk: "; cin >> milk; if (milk <= 0) { cout << donuts << " donuts and NO milk!\n" << "Go buy some milk.\n"; } else { dpg = donuts / (double) milk; cout << donuts << " donuts.\n" << milk << " glasses of milk.\n" << "You have " << dpg << " donuts for each glass of milk.\n"; } cout << “End of program.\n"; } Enter number of donuts: 12 Enter number of glasses of milk: 0 12 donuts and NO milk! Go buy some milk! End of program.">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 16 Exception Handling. What is Exception Handling? A method of handling errors that informs the user of the problem and prevents the program from.

Similar presentations


Presentation on theme: "Chapter 16 Exception Handling. What is Exception Handling? A method of handling errors that informs the user of the problem and prevents the program from."— Presentation transcript:

1 Chapter 16 Exception Handling

2 What is Exception Handling? A method of handling errors that informs the user of the problem and prevents the program from crashing. The program may terminate or do another action to handle the problem. Throwing an exception: Either the standard libraries or your code signals an error. If it is a built-in exception, you can place code to “handle the exception” and allow for correction.

3 Exception-Handling with if-else statement #include using namespace std; void main () { int donuts, milk; double dpg; cout << "Enter number of donuts: "; cin >> donuts; cout << "Enter number of glasses of milk: "; cin >> milk; if (milk <= 0) { cout << donuts << " donuts and NO milk!\n" << "Go buy some milk.\n"; } else { dpg = donuts / (double) milk; cout << donuts << " donuts.\n" << milk << " glasses of milk.\n" << "You have " << dpg << " donuts for each glass of milk.\n"; } cout << “End of program.\n"; } Enter number of donuts: 12 Enter number of glasses of milk: 0 12 donuts and NO milk! Go buy some milk! End of program.

4 Exception Handling with try-throw- catch block #include using namespace std; void main () { int donuts, milk; double dpg; try { cout << "Enter number of donuts: "; cin >> donuts; cout << "Enter number of glasses of milk: "; cin >> milk; if (milk <= 0) { throw donuts; dpg = donuts / (double) milk; cout << donuts << " donuts.\n" << milk << " glasses of milk.\n" << "You have " << dpg << " donuts for each glass of milk.\n"; } catch (int e) { cout << e << “ donuts and NO milk!\n” << “Go buy some milk.\n”; } cout << “End of program.\n"; } Enter number of donuts: 12 Enter number of glasses of milk: 0 12 donuts and NO milk! Go buy some milk! End of program. catch block is the code to execute if the exception happens. It receives the value thrown and acts upon it.

5 Define an exception class #include using namespace std; class NoMilk { public: NoMilk(); NoMilk(int how_many); int getDonuts(); private: int count; }; void main () { int donuts, milk; double dpg; try { cout << "Enter number of donuts: "; cin >> donuts; cout << "Enter number of glasses of milk: "; cin >> milk; if (milk <= 0) throw NoMilk (donuts); dpg = donuts / (double) milk; cout << donuts << " donuts.\n" << milk << " glasses of milk.\n" << "You have " << dpg << " donuts for each glass of milk.\n"; } catch (NoMilk e) { cout << e.getDonut() << “ donuts and NO milk!\n” << “Go buy some milk.\n”; } cout << “End of program.\n"; } NoMilk::NoMilk () : count(0) {} NoMilk::NoMilk (int how_many) : count(how_many) {} int NoMilk::getDonuts() {return count;} Enter number of donuts: 12 Enter number of glasses of milk: 0 12 donuts and NO milk! Go buy some milk! End of program.

6 Define an exception class (cont.) #include using namespace std; class NoMilk { public: NoMilk(); NoMilk(int how_many); int getDonuts(); private: int count; }; class DivideByZero {}; //Empty class void main () { int donuts, milk; double dpg; try { cout << "Enter number of donuts: "; cin >> donuts; cout << "Enter number of glasses of milk: "; cin >> milk; if (milk < 0) throw NoMilk (donuts); else if (milk == 0) throw DivideByZero(); dpg = donuts / (double) milk; cout << donuts << " donuts.\n" << milk << " glasses of milk.\n" << "You have " << dpg << " donuts for each glass of milk.\n"; } catch (NoMilk e) { cout << e.getDonut() << “ donuts and NO milk!\n” << “Go buy some milk.\n”; } catch (DivideByZero) { cout << “Error! Divide By Zero \n” << “ Exiting!\n”; exit(1); } cout << “End of program.\n"; } NoMilk::NoMilk () : count(0) {} NoMilk::NoMilk (int how_many) : count(how_many) {} int NoMilk::getDonuts() {return count;} Enter number of donuts: 12 Enter number of glasses of milk: 0 Error! Divide By Zero Exiting! End of program.

7 Multiple Throws and Catches A try clock can potentially throw any number of exception values. Exception values can be different types. However, only one exception can be thrown in 1 try block. Each catch block can only catch values of 1 type. There can be more than 1 catch block.

8 try-throw-catch syntax try { … //Some codes; throw (DataType1 e_1); … //Some more codes throw (DataType2 e_2); … //Some more codes throw (DataTypen e_N); } catch (DataType1 err1) { … // Code for exception handler 1 } catch (DataType1 err2) { … // Code for exception handler 2 } catch (DataType1 errN) { … // Code for exception handler N } catch (…) … // Code for exception handler for anything not above }

9 Programming Techniques for Exception Handling Include the try-throw within each function and the catch in a different function. Every exception thrown must be caught somewhere in your code. Otherwise the std::terminate() will be called and end your program. –You can nest try-catch blocks. –Don’t overuse. Write code that is robust and only use exceptions when necessary. Better to avoid using exceptions except if knowing how and where the function is used determines how the exception is handled.


Download ppt "Chapter 16 Exception Handling. What is Exception Handling? A method of handling errors that informs the user of the problem and prevents the program from."

Similar presentations


Ads by Google