1 / 19

The C++ Exceptions

The C++ Exceptions. Chien Chin Chen Department of Information Management National Taiwan University. C++ Exceptions. Exception : A mechanism for handling an error during execution . A function can indicate that an error has occurred by throwing an exception .

nancys
Télécharger la présentation

The C++ Exceptions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The C++ Exceptions Chien Chin Chen Department of Information Management National Taiwan University

  2. C++ Exceptions • Exception: • A mechanism for handling an error during execution. • A function can indicate that an error has occurred by throwing an exception. • The code that deals with the exception is said to handle it. • Uses a try block and catch blocks.

  3. C++ Exceptions –Catch Exceptions (1/6) • C++ provides try-catch blocks to catch an exception. • Syntax: Place a statement(s) (function or method) that might throw an exception within a try block. try { statement(s); } catch (ExceptionClass identifier) { statement(s); } catch (ExceptionClass identifier) { statement(s); } Appropriate code to handle the exception. A try block must be followed by one or morecatch blocks.

  4. When a statement (function or method) in a try block causes an exception: Rest of try block is ignored. Control passes to catch block corresponding to the exception. After a catch block executes, control passes to statement after last catch block associated with the try block. C++ Exceptions –Catch Exceptions (2/6) try { ... statement; ... } catch (ExceptionClass identifier) { statement(s); } statement(s); Throw an exception

  5. C++ Exceptions –Catch Exceptions (3/6) • If there is no applicable catch block for an exception, abnormal program termination usually occurs. • If an exception occurs in the middle of a try block, the destructors of all objects local to that block are called. • Ensure that all resources allocated in that block are released.

  6. C++ Exceptions –Catch Exceptions (4/6) • Example: void encodeChar(int i, string& str) { ... str.replace(i, 1, 1, newChar); } Throw exception!! Can throw the out_of_range exception. void encodeString(int numChar, string& str) { for(int i=numChar-1; i>=0; i--) encodeChar(i,str); } Throw exception!! int main() { string str1 = “NTU IM”; encodeString(99, str1); return 0; } Abnormal program termination

  7. C++ Exceptions –Catch Exceptions (5/6) void encodeChar(int i, string& str) { try { ... str.replace(i, 1, 1, newChar); } catch (out_of_range e) { cout << “No character at ” << i << endl; } } No character at 98 No character at 97 … void encodeString(int numChar, string& str) { for(int i=numChar-1; i>=0; i--) encodeChar(i,str); } int main() { string str1 = “NTU IM”; encodeString(99, str1); return 0; }

  8. C++ Exceptions –Catch Exceptions (6/6) void encodeChar(int i, string& str) { ... str.replace(i, 1, 1, newChar); } void encodeString(int numChar, string& str) { try { for(int i=numChar-1; i>=0; i--) encodeChar(i,str); } catch (out_of_range e) { cout << “Something wrong” << endl; } } Something wrong int main() { string str1 = “NTU IM”; encodeString(99, str1); return 0; }

  9. C++ Exceptions –Throwing Exceptions (1/4) • When you detect an error within a method, you can throw an exception by using a throw statement. • Syntax: throwExceptionClass(stringArgument); type of exception More detailed information void myMethod(int x) throw(MyException) { if (...) throw MyException(“MyException: …”); ... } // end myMethod

  10. C++ Exceptions –Throwing Exceptions (2/4) • When a throw statement executes: • The remaining code within the function does not execute. • The exception is propagated back to the point where the function was called. try { ... myMethod(int x); ... } catch (ExceptionClass identifier) { statement(s); } back to here!!

  11. C++ Exceptions –Throwing Exceptions (3/4) • Functions that throw an exception have a throw clause. • To restrict the exceptions that a function can throw. • Omit a throw clause allows a function to throw any exception. void myMethod(int x) throw(MyException1, MyException2) { if (...) throw MyException1(“MyException: …”); ... if (...) throw MyException2(“MyException: …”); ... } // end myMethod

  12. C++ Exceptions –Throwing Exceptions (4/4) • The documentation of a function (or method) should indicate any exception it might throw.

  13. C++ Exceptions –Defining Your Own Exceptions (1/2) • C++ Standard Library supplies a number of exception classes. • E.g., exception, out_of_range, … etc. • See appendix A.9. • You may also want to define your own exception class. • Should inherit from those pre-defined exception classes for a standardized exception working interface. • Note: • #include <stdexcept> • Using namespace std;

  14. C++ Exceptions –Defining Your Own Exceptions (2/2) • Example: #include <exception> #include <string> using namespace std; class MyException : public exception { public: MyException(const string & Message = “”) : exception(Message.c_str()) {} } // end class try { ... } catch (MyExceptoin e) { cout << e.what(); } throw MyException(“more detailed information”);

  15. An ADT List Implementation Using Exceptions (1/5) • Exceptions: • An out-of-bound list index. • An attempt to insert into a full list.

  16. An ADT List Implementation Using Exceptions (2/5) • We define two exception classes: • #include <stdexcept> • #include <string> • using namespace std; • class ListIndexOutOfRangeException :public out_of_range • { • public: • ListIndexOutOfRangeException(const string& message = “”) • : out_of_range(message.c_str()){} • }; // end ListException

  17. An ADT List Implementation Using Exceptions (3/5) • #include <stdexcept> • #include <string> • using namespace std; • class ListException :public logic_error • { • public: • ListException(const string & message = “”) • : logic_error(message.c_str()) {} • }; // end ListException

  18. An ADT List Implementation Using Exceptions (4/5) #include “ListException.h” #include “ListIndexOutOfRangeException.h” . . . class List { public: . . . void insert(int index, const ListItemType& newItem) throw(ListIndexOutOfRangeException, ListException); . . . } // end List Indicate that the insert operation may throw out-of-range and full exceptions.

  19. An ADT List Implementation Using Exceptions (5/5) void List::insert(int index, const ListItemType& newItem) throw(ListIndexOutOfRangeException, ListException) { if (size >= MAX_LIST) throw ListException("ListException: List full on insert"); if (index >= 1 && index <= size+1) { for (int pos = size; pos >= index; --pos) items[translate(pos+1)] = items[translate(pos)]; // insert new item items[translate(index)] = newItem; ++size; // increase the size of the list by one } else // index out of range throw ListIndexOutOfRangeException( "ListIndexOutOfRangeException: Bad index on insert"); } // end insert

More Related