Slide 1:
Exceptions
General Description
-
A way of dealing with errors
-
Calling functions must deal with
-
Caller cannot simply ignore them
Shawn Stoffer -- CS151 Spring 2001
Slide 2:
Exceptions
General Description
They are used to deal with errors.
If:
-
something is passed in that is incorrect
-
unusual data is returned by a function called
Then:
instead of returning a garbage value, from a
function, that can be ignored,
throw an exception, because it must be dealt with.
Shawn Stoffer -- CS151 Spring 2001
Slide 3:
Exceptions
Behavior
-
Exceptions, once thrown immediately exit all functions, until
caught
Shawn Stoffer -- CS151 Spring 2001
Slide 4:
Exceptions
Behavior (continued)
Slide 5:
Exceptions
Behavior (continued)
-
When using exceptions, you must be careful, as if you do
not catch them, they will cause your program to terminate.
Shawn Stoffer -- CS151 Spring 2001
Slide 6:
Exceptions
Syntax
-
throw <exception variable>;
-
try { <statements> } catch (<exception variable>)
{ <statements> }
Shawn Stoffer -- CS151 Spring 2001
Slide 7:
Exceptions
Syntax
throw <exception
variable>;
You can 'throw'
anything, a value (1, 4.5, 9.0, etc...), or a variable.
Whatever you
'throw' is passed with the exception through the functions which called
the offending function. (read: offending function = function that
threw the exception).
Shawn Stoffer -- CS151 Spring 2001
Slide 8:
Exceptions
Syntax
Example
int getElement(int rows) {
int i = 5;
if (rows > MAXROWS) {
throw
5;
}
return table[rows];
}
Shawn Stoffer -- CS151 Spring 2001
Slide 9:
Exceptions
Syntax
Example
enum Excepts
{
OutOfRangeException
};
int getElement(int rows)
{
Excepts i = OutOfRangeException;
if (rows > MAXROWS)
{
throw
i;
}
return table[rows];
}
Shawn Stoffer -- CS151 Spring 2001
Slide 10:
Exceptions
Syntax
try { <statements> } catch (<exception
variable>) { <statements> }
Shawn Stoffer -- CS151 Spring 2001
Slide 11:
Exceptions
Syntax
try { <statements> } catch (<exception
variable>) { <statements> }
-
try { <statements> }
-
statements are normal C++ statements that you want to try,
but might throw an exception
-
because they might throw an exception, you want to put them
in a try 'block'
Shawn Stoffer -- CS151 Spring 2001
Slide 12:
Exceptions
Syntax
try { <statements> } catch (<exception
variable>) { <statements> }
-
catch (<exception variable>)
-
looks like a function description
-
i.e. catch (int e)
Shawn Stoffer -- CS151 Spring 2001
Slide 13:
Exceptions
Syntax
try { <statements> } catch (<exception
variable>) { <statements> }
-
catch (<exception variable>)
-
looks like a function description
-
i.e. catch (Excepts e)
Shawn Stoffer -- CS151 Spring 2001
Slide 14:
Exceptions
Syntax
try { <statements> } catch (<exception
variable>) { <statements> }
-
catch (<exception variable>)
-
looks like a function description
-
but will only catch exceptions which are of the type defined...
-
so, if the <statements> of the try block throw an int,
then you must catch an int.
Shawn Stoffer -- CS151 Spring 2001
Slide 15:
Exceptions
Syntax
try { <statements> } catch (<exception
variable>) { <statements> }
-
catch (<exception variable>)
-
looks like a function description
-
you may have multiple catch statements, for different types
of exceptions:
-
i.e.
catch (int e) { /* do something */ }
catch (float e) { /* do something else */ }
Shawn Stoffer -- CS151 Spring 2001
Slide 16:
Exceptions
Syntax
try { <statements> } catch (<exception
variable>) { <statements> }
-
catch (<exception variable>) { <statements> }
-
<statements> are normal C++ statements
-
what to do in case this exception is thrown...
Shawn Stoffer -- CS151 Spring 2001
Slide 17:
Exceptions
Syntax (example)
try { <statements> } catch (<exception
variable>) { <statements> }
void afunc(int e) throw(int)
{
if (e > 5)
{
throw
-1;
}
}
int i = 9;
try {
afunc(i);
} catch (int e) {
if (e == -1) {
cout <<
"We encountered the exception for an invalid value!" << endl;
return;
}
} catch (...) {
cout << "An unknown exception
was found!!" << endl;
throw;
}
Shawn Stoffer -- CS151 Spring 2001
Slide 18:
Exceptions
Syntax (example)
try { <statements> } catch (<exception
variable>) { <statements> }
-
void afunc(int e) throw(int)
-
What is throw(int)???
-
This tells the compiler that the function may throw an exception,
and is absolutely necessary for valid C++.
-
Also warns someone using that function what types of exceptions
that they have to expect to catch.
Shawn Stoffer -- CS151 Spring 2001
Slide 19:
Exceptions
Syntax (example)
try { <statements> } catch (<exception
variable>) { <statements> }
-
void afunc(int e) throw(int)
-
What is throw(int)???
-
This tells the compiler that the function may throw an exception,
and is absolutely necessary for valid C++.
-
Also warns someone using that function what types of exceptions
that they have to expect to catch.
Shawn Stoffer -- CS151 Spring 2001
Slide 20:
Exceptions
Syntax (example)
try { <statements> } catch (<exception
variable>) { <statements> }
-
catch (...) {
-
What is (...)???
-
This tells C++ to catch ANY other type of exception that
is thrown by the code.
-
Though, then you have to deal with it...
Shawn Stoffer -- CS151 Spring 2001
Slide 21:
Exceptions
Syntax (example)
try { <statements> } catch (<exception
variable>) { <statements> }
-
throw;
-
What is 'throw;' throwing???
-
If a throw is by itself in a catch block... then throw will
re-'throw' the exception that it caught...
-
so if you don't know what to do, or simply want to acknowledge
the exception, but can't deal with it, then re-'throw' it.
Shawn Stoffer -- CS151 Spring 2001