160 likes | 216 Views
Exceptions and Error Handling. try , catch , and throw. void main() { try { Func1(); } catch ( int ) { cout << "caught the object<br>"; } } void Func1() { Func2(); } void Func2() { Func3(); } void Func3() { int i = 0; throw i; }. Exceptions.
E N D
Exceptions and Error Handling Lecture By: Mr. Hari Mohan Pandey
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
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
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
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
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
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
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
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
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
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
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
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
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
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