| |||||||||||
| |||||||||||
Creating exception objects, throwing and catching them
Exceptions example from a Well House Consultants training course
More on Exceptions [link]
Source code: CppExcept.cpp Module: C236
#include <iostream>
/* Exceptions in C++ Rather than check for every error you can think of - only to have your program fail when an error you *didn't* or *couldn't* check for occurs, you can use an exception system in C++. Exceptions are also used for returning abnormal conditions from functions - if you ask (for example) a function for the cost of a stuffed platypus, but the shop doesn't sell stuffed playtpusses, then you'll probably want to throw an exception object */ */ using namespace std; // Define some classes of exceptions class CppExcept { public: CppExcept(const char* pStr = "Default Message") : pMessage(pStr) {}; const char* what() const {return pMessage;} private: const char* pMessage; }; // You can throw any object - even an empty container! class Chris { } ; // function to illustrate exception being throws out of a function int more(int j) { cout << "Hello "; cout << "World " << endl; if (j == 1) throw CppExcept("Life and the Universe"); if (j == 0) throw Chris(); return 42; } // main program int main() { cout << "Showing Exceptions in C++" << endl; cout << "-------------------------" << endl; for(int i = 0 ; i < 4 ; i++) { // Block of code we want to try to run ... try { if (i == 0) throw CppExcept(); else if (i == 1) throw CppExcept("Nobody knows the trouble I've seen..."); else std::cout << "we done fine" << std::endl; std::cout << "we have finished the block" << std::endl; } // And if it failed (threw an exception) while running, catch the exception ... catch(exception &t) { cout << endl << "Standard Exception: " << t.what() << endl; } catch(const CppExcept& t) { cout << endl << "Exception: " << t.what() << endl; } } cout << "----------------------" << endl; cout << "Program still running!" << endl; cout << "----------------------" << endl; for(int i = 0 ; i < 3 ; i++) { // Call a function which may also throw an exception try { int rz = more(i); cout << rz << endl; } // If it failed to return an int, sort out the problem catch(const CppExcept& t) { cout << endl << "Having trouble with more - " << t.what() << endl; } catch(const Chris& t) { cout << endl << "What an interesting object!" << endl; } } // This call to "more" will throw an exception. // As it is NOT in a try clause, the program will trap and abort and the // final "cout" message will not be displayed. int nah = more(1); cout << "Should not get here!" << endl; return 0; } /* Example of that running ... 92:antrim graham$ g++ -o CppExcept CppExcept.cpp 92:antrim graham$ ./CppExcept Showing Exceptions in C++ ------------------------- Exception: Default Message Exception: Nobody knows the trouble I've seen... we done fine we have finished the block we done fine we have finished the block ---------------------- Program still running! ---------------------- Hello World What an interesting object! Hello World Having trouble with more - Life and the Universe Hello World 42 Hello World terminate called after throwing an instance of 'CppExcept' Abort trap 92:antrim graham$ */ Learn about this subject
This module and example are covered on the following public courses:
* Learning to program in C and C++ * C++ for C Programmers * C and C++ Programming * Learning to program in C and C++ * C and C++ Programming Also available on on site courses for larger groups Books covering this topic
Yes. We have over 700 books in our library. Books
covering C and C++ are listed here and when you've selected a
relevant book we'll link you on to Amazon to order.
Other Examples
This example comes from our "Exceptions" training module. You'll find a description of the topic and some
other closely related examples on the "Exceptions" module index page.
Full description of the source code
You can learn more about this example on the training courses listed on this page,
on which you'll be given a full set of training notes.
Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License. Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre. • The Horse's mouth provides a daily tip or thought. • Further resources are available via the resources centre. • All of these resources can be searched through through our search engine • And there's a global index here. Web site author
Purpose of this website
This is a sample program, class demonstration or answer from a
training course. It's main purpose
is to provide an after-course service to customers who have attended our
public private or
on site courses, but the examples are made
generally available under conditions described below.
Conditions of use
Past attendees on our training courses are welcome to use individual
examples in the course of their programming, but must check
the examples they use to ensure that they are suitable for their
job. Remember that some of our examples show you how not to do
things - check in your notes. Well House Consultants take no responsibility
for the suitability of these example programs to customer's needs.
This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details. Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request. |
| ||||||||||
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho PAGE: http://www.wellho.net/resources/ex.php • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb |