Overview of Exception Handling in C++ Programming
This content provides insights into exception handling in C++ programming, specifically comparing it to Java. It covers the differences in exception handling between C++ and Java, such as the absence of null pointer exceptions and divide-by-zero exceptions in C++. It explains how C++ deals with exceptions, highlighting the lack of a finally block and the use of catch statements. Additionally, it demonstrates examples of handling exceptions in C++ using strings and objects of existing classes.
Download Presentation
Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
E N D
Presentation Transcript
CSC 270 Survey of Programming Languages C++ Lecture 6 Exceptions
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 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) { 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 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 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 {}; {
ExceptionDemo.cpp creation of the Error classes #include #include using namespace std; <iostream> <string> class NegativeNumber public: NegativeNumber(void) {} NegativeNumber(string theMessage) : message(theMessage) {} string getMessage() const {return message; } private: string message; }; class DivideByZero {}; {
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.
int { main(void) int double pencils, erasers; ppe; //pencils per eraser try { cout << "How many pencils do you" << " have?\n"; cin >> pencils; if (pencils < 0) throw NegativeNumber("pencils");
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; 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; }
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);
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