Chapter 16 Exception Handling What is Exception Handling

  • Slides: 9
Download presentation
Chapter 16 Exception Handling

Chapter 16 Exception Handling

What is Exception Handling? • A method of handling errors that informs the user

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.

Exception-Handling with if-else statement #include <iostream> using namespace std; void main () { int

Exception-Handling with if-else statement #include <iostream> 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.

Exception Handling with try-throwcatch block #include <iostream> using namespace std; void main () {

Exception Handling with try-throwcatch block #include <iostream> 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.

Define an exception class cout << donuts << " donuts. n" << milk <<

Define an exception class cout << donuts << " donuts. n" << milk << " glasses of milk. n" << "You have " << dpg << " donuts for each glass of milk. n"; } catch (No. Milk e) { cout << e. get. Donut() << “ donuts and NO milk!n” << “Go buy some milk. n”; } cout << “End of program. n"; #include <iostream> using namespace std; class No. Milk { public: No. Milk(); No. Milk(int how_many); int get. Donuts(); 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 No. Milk (donuts); dpg = donuts / (double) milk; } No. Milk: : No. Milk () : count(0) {} No. Milk: : No. Milk (int how_many) : count(how_many) {} int No. Milk: : get. Donuts() {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.

Define an exception class (cont. ) cout << donuts << " donuts. n" <<

Define an exception class (cont. ) cout << donuts << " donuts. n" << milk << " glasses of milk. n" << "You have " << dpg << " donuts for each glass of milk. n"; } catch (No. Milk e) { cout << e. get. Donut() << “ donuts and NO milk!n” << “Go buy some milk. n”; } catch (Divide. By. Zero e) { cout << “Error! Divide By Zero n” << “ Exiting!n”; exit(1); } cout << “End of program. n"; #include <iostream> using namespace std; class No. Milk { public: No. Milk(); No. Milk(int how_many); int get. Donuts(); private: int count; }; class Divide. By. Zero {}; //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 No. Milk (donuts); else if (milk == 0) throw Divide. By. Zero(); dpg = donuts / (double) milk; } No. Milk: : No. Milk () : count(0) {} No. Milk: : No. Milk (int how_many) : count(how_many) {} int No. Milk: : get. Donuts() {return count; } Enter number of donuts: 12 Enter number of glasses of milk: 0 Error! Divide By Zero Exiting! End of program.

Multiple Throws and Catches • A try block can potentially throws any number of

Multiple Throws and Catches • A try block can potentially throws 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.

try-throw-catch syntax try { … //Some codes; throw (Data. Type 1 e_1); … //Some

try-throw-catch syntax try { … //Some codes; throw (Data. Type 1 e_1); … //Some more codes throw (Data. Type 2 e_2); … //Some more codes throw (Data. Typen e_N); } catch (Data. Type 1 err 1) { … // Code for exception handler 1 } catch (Data. Type 1 err 2) { … // Code for exception handler 2 } catch (Data. Type 1 err. N) { … // Code for exception handler N } catch (…) … // Code for exception handler for anything not above }

Programming Techniques for Exception Handling • Include the try-throw within each function and the

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.