1 / 15

Exceptions and Error Handling

Exceptions and Error Handling. try , catch , and throw. void main() { try { Func1(); } catch ( int ) { cout &lt;&lt; &quot;caught the object<br>&quot;; } } void Func1() { Func2(); } void Func2() { Func3(); } void Func3() { int i = 0; throw i; }. Exceptions.

jsharrow
Download Presentation

Exceptions and Error Handling

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. Exceptions and Error Handling Lecture By: Mr. Hari Mohan Pandey

  2. try,catch, and throw void main() { try { Func1(); } catch(int) { cout << "caught the object\n"; } } void Func1() { Func2(); } void Func2() { Func3(); } void Func3() { int i = 0; throw i; } Lecture By: Mr. Hari Mohan Pandey

  3. Exceptions • Exceptions are objects which are created for the purpose of “throwing” and “catching” them from one part of the program to another. • Objects are “exceptions” only in the way they are used, not the way they are defined. • Any object can be thrown. • Variables of any type can be thrown. Lecture By: Mr. Hari Mohan Pandey

  4. Exceptions • Exceptions were created to handle error conditions. • However, they are just another control-flow technique, and are not restricted to the handling of error conditions. • Throwing and catching objects is a way of moving through the call stack, a set of nested function calls. Lecture By: Mr. Hari Mohan Pandey

  5. try,catch, and throw • Place a try block around the code from which you will throw, usually a function call. • Place the catch block(s) immediately after the try block. • Place the throw statement at the place from which you wish to jump to the catch. Lecture By: Mr. Hari Mohan Pandey

  6. trycatch and throw void main() { try { Func1(); } catch(int) { cout << "caught the object\n"; } } void Func1() { Func2(); } void Func2() { Func3(); } void Func3() { int i = 0; throw i; } Lecture By: Mr. Hari Mohan Pandey

  7. Multiple catches • catch statements check the type of the object, and can catch only one type. • Thus we can have multiple catches after a single try, to catch various types of thrown objects. • Multiple catches work like switch statements, and include a default: catch(…) Lecture By: Mr. Hari Mohan Pandey

  8. Multiple catches try { Func4(); } catch(char) { cout << "caught a char/n”; } catch(int) { cout << "caught an int/n”; } catch(MyObject) { cout << "caught a MyObject/n”; } catch(...) { cout << "caught something/n"; } Lecture By: Mr. Hari Mohan Pandey

  9. Multiple catches void Func4() { char c = 'c', buffer[80]; int i = 0; float f = 0; MyObject mo; cout << "name the throw type: "; cin >> buffer; if (!strcmp("char",buffer)) throw c; else if (!strcmp("int",buffer)) throw i; else if (!strcmp("MyObject",buffer)) throw mo; else throw f; } Lecture By: Mr. Hari Mohan Pandey

  10. Using Caught Objects • If we name a variable of the type we catch, we can use it in the catch block: catch(int i) { cout << "caught an int: " << i << endl; } Lecture By: Mr. Hari Mohan Pandey

  11. Making a Thrown Object • An object can be made in the throw statement by naming the constructor: • class MyObject contains no user defined member data or functions, but it is provided with a default constructor, copy constructor, destructor, and assignment operator. class MyObject {}; void FuncX() { throw MyObject(); } Lecture By: Mr. Hari Mohan Pandey

  12. Exceptions • Exceptions are objects of classes created for no other purpose than being thrown. class Error1 {}; class Error2 {}; class Error3 {}; void Func() { ... if (ErrorType == 1) throw Error1(); else if (ErrorType == 2) throw Error2(); else if (ErrorType == 3) throw Error3(); } Lecture By: Mr. Hari Mohan Pandey

  13. Exceptions • We create dummy exceptions of various classes, because the catch statements can discriminate between the different types: try { Func(); } catch(Error1) { cout << "caught Error type 1" << endl; } catch(Error2) { cout << "caught Error type 2" << endl; } catch(Error3) { cout << "caught Error type 3" << endl; } Lecture By: Mr. Hari Mohan Pandey

  14. Data in Exceptions • We use different types of objects to identify different types of errors, because the catch statements can discriminate types. • But we may want to pass additional data about the error. • This can be done by using exception objects that include member data. Lecture By: Mr. Hari Mohan Pandey

  15. Data in Exceptions class Error { public: void SetSize(int size) { ErrorSize = size; } int GetSize() const { return ErrorSize; } private: int ErrorSize; }; void main() { try { Func6(); } catch(Error anError) { cout << anError.GetSize() << endl; } } void Func6() { Error MyError; MyError.SetSize(32); throw MyError; } Lecture By: Mr. Hari Mohan Pandey

More Related