Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 270 – Survey of Programming Languages

Similar presentations


Presentation on theme: "CSC 270 – Survey of Programming Languages"— Presentation transcript:

1 CSC 270 – Survey of Programming Languages
C++ Lecture 6 – Exceptions

2 Difference from Java Fewer exceptions
No null pointer exception No divide by zero exception Undefined behavior instead No finally block (try, catch, finally) Catch all statements Standard: inherit from std::exception or std::runtime_error (for what) Use catch (...) for all Use catch(const char* Message) for a string No concept of checked exceptions

3 See no divide by zero error
#include <iostream> #include <string> using namespace std; int main(void) { int pencils, erasers; double ppe; //pencils per eraser cout << "How many pencils do you" << " have?\n"; cin >> pencils; cout << "How many erasers do you" << " have?\n"; cin >> erasers; ppe = pencils / (double) erasers; cout << "Each eraser must last through " << ppe << " pencils." << endl; cout << "End of program." << endl; return(0); }

4 Just Throw a String #include <iostream> #include <string>
using namespace std; int main (void) { int pencils, erasers; double ppe; //pencils per eraser cout << "How many pencils do you" << " have?\n"; cin >> pencils; cout << "How many erasers do you" << " have?\n"; cin >> erasers; try if (erasers == 0) throw "divided by zero"; } ppe = pencils / (double) erasers; cout << "Each eraser must last through " << ppe << " pencils." << endl ; cout << "End of program." << endl; catch (const char *Message) cout << Message << endl; return (0);

5 Or throw object of Existing Class
#include <stdexcept> #include <iostream> #include <string> using namespace std; int main (void) { int pencils, erasers; double ppe; //pencils per eraser cout << "How many pencils do you" << " have?\n"; cin >> pencils; cout << "How many erasers do you" << " have?\n"; cin >> erasers; try if (erasers == 0) throw runtime_error("a message"); } ppe = pencils / (double) erasers; cout << "Each eraser must last through " << ppe << " pencils." << endl; cout << "End of program." << endl; catch (runtime_error e) cout << e.what() << endl; return (0);

6 Throw an error object you create Step 1: Create an error class
Create a small class named as the error type Empty or filled is fine using namespace std; class NegativeNumber { public: NegativeNumber(void) {} NegativeNumber(string theMessage) : message(theMessage) {} string getMessage() const {return message; } private: string message; }; class DivideByZero {};

7 ExceptionDemo.cpp creation of the Error classes
#include <iostream> #include <string> using namespace std; class NegativeNumber { public: NegativeNumber(void) {} NegativeNumber(string theMessage) : message(theMessage) {} string getMessage() const {return message; } private: string message; }; class DivideByZero {};

8 Throw an error object you create Step 2: Throw when you find error
Use the throw command Follow it by an object of an error class Throw will Stop executing the method it is in give that object back to the calling program If it is not caught, it will crash the program.

9 int main(void) { int pencils, erasers; double ppe; //pencils per eraser try { cout << "How many pencils do you" << " have?\n"; cin >> pencils; if (pencils < 0) throw NegativeNumber("pencils");

10 Catch the error Surround the call to the method that may throw an error with a try { } After the try, add a catch block Catch ( what you want to catch and its var name){ } Inside the catch block, you can access the variables inside the error object

11 cout << "How many erasers do you" << " have
cout << "How many erasers do you" << " have?\n"; cin >> erasers; if (erasers < 0) throw NegativeNumber("erasers"); if (erasers != 0) ppe = pencils / (double) erasers; else throw DivideByZero(); cout << "Each eraser must last through " << ppe << " pencils." << endl; }

12 catch(NegativeNumber e) { cout << "Cannot have a negative number of " << e.getMessage() << endl; } catch(DivideByZero) { cout << "Do not make any mistakes!" << endl; cout << "End of program." << endl; return(0);

13 Summary At error – throw exception At use of the method: String
Existing exception Your own class empty Your own class with data At use of the method: Try { } surrounding all that should not be done for error Catch ( ) { } do only if error If standard error, use what method If custom use your method … to catch all


Download ppt "CSC 270 – Survey of Programming Languages"

Similar presentations


Ads by Google