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.

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Odds And Ends – Switch Statement rIt is often necessary in programs to set up a set of cases, where.
Advertisements

Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 18 Exception Handling. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Exception Handling Basics Defining.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Computer Science 1620 Math Library. Remember this program? suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to.
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Chapter 12 Separate Compilation and Namespaces. Abstract Data Type (ADT) ADT: A data type consisting of data and their behavior. The abstraction is that.
Factorial Preparatory Exercise #include using namespace std; double fact(double); int fact(int); int main(void) { const int n=20; ofstream my_file("results.txt");
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Vectors, lists and queues
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne
Derived data types Dealing with data –Where the information is stored –What value is kept there –What kind of information is stored Address operator Pointers.
Revision.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 5 Functions for All Subtasks.
第三次小考. #include using namespace std; int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Tinaliah, S. Kom.. * * * * * * * * * * * * * * * * * #include using namespace std; void main () { for (int i = 1; i
Exception Handling – illustrated by Java mMIC-SFT November 2003 Anders P. Ravn Aalborg University.
Triana Elizabeth, S.Kom. #include using namespace std; void main () { for (int i = 1; i
Lecture 9. 2 Exception  An exception is a unusual, often unpredictable event, detectable by software or hardware, that requires special processing occurring.
1 Chapter 17-2 Templates and Exceptions Dale/Weems.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 16 Exception Handling.
CSIS 123A Lecture 11 Exception Handling. Introduction  Typical approach to development:  Write programs assuming things go as planned  Get ‘core’ working.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 16 Exception Handling.
Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  due to poor understanding of the problem and solution.
Exception Handling Introduction Exception handling is a mechanism to handle exceptions. Exceptions are error like situations. It is difficult to decide.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Handling ErrorstMyn1 Handling Errors Up to this point we haven't worried much about errors or exceptions. First, let's distinguish between errors and exceptions.
C++ Exceptions STL Vector. Example int Quotient (int numer, int denom} { if (denom != 0) return (numer/denom); else //What to do?? }
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
CSC 270 – Survey of Programming Languages C++ Lecture 6 – Exceptions.
Exceptions cs1043. Program Exceptions When a program detects an error, what should it do? – Nothing, simply allow the program to fail. – Implement a course.
1 Chapter 4: Basic Control Flow ► Chapter Goals  To be able to implement decisions using if statements  To understand statement blocks  To learn how.
(13-1) Exception Handling in C++ D & D Chapter 17 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Exceptions & Templates
Exception Handling Fall 2008 Dr. David A. Gaitros
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
C++ Namespaces, Exceptions CSci 588: Data Structures, Algorithms and Software Design All material not from online sources copyright © Travis Desell, 2011.
Exception Handling How to handle the runtime errors.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Object-Oriented Programming in C++ Exception.
C++ Lesson 1.
Lab 6 Instructions You can use g++ on build server, visual studio on local machine or your preferred C++ IDE. Important Note: For your grade, please show.
C ++ MULTIPLE CHOICE QUESTION
Exception Handling C++.
Chapter 16 Exception Handling
Andy Wang Object Oriented Programming in C++ COP 3330
Why exception handling in C++?
EXCEPTION HANDLING.
Object Oriented Programming COP3330 / CGS5409
Learning Objectives What else in C++ Bitwise operator
CSC1201: Programming Language 2
Exceptions with Functions
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 17 Templates and Exceptions Part 2
Starting Out with C++: From Control Structures through Objects
Exercise 11.1 Write a code fragment that performs the same function as the statement below without using the crash method Toolbox.crash(amount < 0,
CSC 270 – Survey of Programming Languages
CSC1201: Programming Language 2
Tenth step for Learning C++ Programming
Functions Imran Rashid CTO at ManiWeber Technologies.
Lecture 9.
Basic Exception Handling
Presentation transcript:

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 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 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-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.

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.

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.

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.

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 }

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.