Title: Exceptions and Error Handling
1Exceptions and Error Handling
2try, catch, and throw
void main() try Func1() catch(int) co
ut ltlt "caught the object\n" void Func1()
Func2() void Func2() Func3() void
Func3() int i 0 throw i
3Exceptions
- 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.
4Exceptions
- 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.
5try, 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.
6try catch and throw
void main() try Func1() catch(int) co
ut ltlt "caught the object\n" void Func1()
Func2() void Func2() Func3() void
Func3() int i 0 throw i
7Multiple 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()
8Multiple catches
try Func4() catch(char) cout ltlt "caught a
char/n catch(int) cout ltlt "caught an
int/n catch(MyObject) cout ltlt "caught a
MyObject/n catch(...) cout ltlt "caught
something/n"
9Multiple catches
void Func4() char c 'c', buffer80 int
i 0 float f 0 MyObject mo cout ltlt
"name the throw type " cin gtgt buffer if
(!strcmp("char",buffer)) throw c else if
(!strcmp("int",buffer)) throw i else if
(!strcmp("MyObject",buffer)) throw
mo else throw f
10Using Caught Objects
- If we name a variable of the type we catch, we
can use it in the catch block
catch(int i) cout ltlt "caught an int " ltlt i ltlt
endl
11Making 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()
12Exceptions
- 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()
13Exceptions
- We create dummy exceptions of various classes,
because the catch statements can discriminate
between the different types
try Func() catch(Error1) cout ltlt "caught
Error type 1" ltlt endl catch(Error2) cout ltlt
"caught Error type 2" ltlt endl catch(Error3) c
out ltlt "caught Error type 3" ltlt endl
14Data 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.
15Data 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 ltlt anError.GetSize() ltlt endl void
Func6() Error MyError MyError.SetSize(32) th
row MyError