CS210 Lab: Stacks--Exception Handling
Overview:
What is an Exception?
- An Exception is an unusual event, often an error, that requires special processing.
- In this lab, the Exception is that the stack is full.
How do you handle an Exception?
Use try-catch and throw statements.
- a try clause encloses the code segment in which an
exception might occur.
- throw signals the fact that an exception has occurred
and control should pass to the exception handler.
- catch processes the thrown exception.
Each catch clause is an exception handler.
Throw Statement
To throw (or raise) an exception, the programmer uses a throw statement, whose syntax is as follows:
throw Expession;
Specifically, in this lab (in StackType.cpp) two exceptions are thrown:
throw FullStack();
and
throw EmptyStack();
Notice, that these are both "exception classes" which have been defined in StackType.h:
class FullStack
// Exception class thrown by Push when stack is full.
{};
class EmptyStack
// Exception class thrown by Pop and Top when stack is emtpy.
{};
Try-Catch Statement
If one part of a program throws an exception, another part should catch the
exception and process it.
This is done with the try-catch statement.
The general syntax of a try-catch statement is as follows:
try
Block
catch ( FormalParameter )
Block
catch ( FormalParameter )
Block
.
.
.
In other words, the syntax is a try clause followed by one or more
catch clauses. The "Block" may be any number of statements enclosed
by a pair of {}.
When a statement or group of statements might result in an exception,
we enclose them in a try clause. For each type of exception that
can be produced by the statements in the try clause, we write a
catch clause (exception handler).
Make sure that all exceptions are caught. An uncaught exception results in
program termination with an error message.
Example Code of Try-Catch and Throw
The following is a complete code which shows how you can put Try-Catch and Throw together:
#include <iostream>
#include <string>
using namespace std;
int Dividing( int, int);
class ZeroDiv // Exception class
{};
int main()
{
int num; // Numerator
int num2; //Denominator
float quo;
cout << "Enter numerator: ";
cin >> num;
cout<< "Enter Denominator: ";
cin >> num2;
try
{
if (num2==0)
throw ZeroDiv();
quo = num/num2;
cout<< "Their quotient is : " <<quo<<endl;
}
catch (ZeroDiv)
{
cout << "Can't divide by zero" << endl;
}
return 0;
}
Lab Specific Hints
The only file that you will modify is Balanced.cpp
- Add a try clause
- Add a catch clause for FullStack, you don't need to add throw FullStack() since it is given in StackType.cpp
- Special Note:
The hardest part is probably deciding which statements
will be enclosed in the try clause. Things to note are:
- If no exceptions are thrown, then the
catch clauses are skipped and control goes to the
statement following the entire try-catch statement.
- However, if an exception is thrown by a statement in
the try clause, control immediately transfers to the
appropriate exception handler (catch clause). That
means that, if there are statements in the try clause
following the one that caused the exception,
they are skipped.
Back to Stack Lab click
here
Copyright: Department of Computer Science, University of Regina.