CSC 270 Survey of Programming Languages C Lecture

  • Slides: 13
Download presentation
CSC 270 – Survey of Programming Languages C++ Lecture 6 – Exceptions

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

Difference from Java • Fewer exceptions – No null pointer exception – No divide

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

See no divide by zero error #include <iostream> #include <string> using namespace std; int

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); }

Just Throw a String #include <iostream> #include <string> using namespace std; int main (void)

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); }

Or throw object of Existing Class #include <stdexcept> #include <iostream> #include <string> using namespace

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); }

Throw an error object you create Step 1: Create an error class • Create

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 Negative. Number { public: Negative. Number(void) {} Negative. Number(string the. Message) : message(the. Message) {} string get. Message() const {return message; } private: string message; }; class Divide. By. Zero {};

Exception. Demo. cpp creation of the Error classes #include <iostream> #include <string> using namespace

Exception. Demo. cpp creation of the Error classes #include <iostream> #include <string> using namespace std; class Negative. Number { public: Negative. Number(void) {} Negative. Number(string the. Message) : message(the. Message) {} string get. Message() const {return message; } private: string message; }; class Divide. By. Zero {};

Throw an error object you create Step 2: Throw when you find error •

Throw an error object you create Step 2: Throw when you find error • Use 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.

int { main(void) int pencils, erasers; double ppe; //pencils per eraser try { cout

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 Negative. Number("pencils");

Catch the error • Surround the call to the method that may throw an

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

cout << "How many erasers do you" << " have? n"; cin >> erasers;

cout << "How many erasers do you" << " have? n"; cin >> erasers; if (erasers < 0) throw Negative. Number("erasers"); if (erasers != 0) ppe = pencils / (double) erasers; else throw Divide. By. Zero(); cout << "Each eraser must last through " << ppe << " pencils. " << endl; }

catch(Negative. Number e) { cout << "Cannot have a negative number of " <<

catch(Negative. Number e) { cout << "Cannot have a negative number of " << e. get. Message() << endl; } catch(Divide. By. Zero) { cout << "Do not make any mistakes!" << endl; } cout << "End of program. " << endl; return(0);

Summary • At error – throw exception – – String Existing exception Your own

Summary • At error – throw exception – – 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