Exception Handling
The CS 240 Utilities provide some classes that help you handle exceptions in your programs.
The most important of these classes is CS240Exception.
CS240Exception serves as the base class for all other exception classes that you might create.
When you throw a CS240Exception object, you can initialize it with a string message that
provides more information about the error that has occurred.
CS240Exception also provides a method for retrieving the message stored in an exception object.
The code below shows how a program can handle exceptions of type CS240Exception.
int foo(int x) {
if (x < 0) {
throw CS240Exception("negative numbers are illegal");
}
return x * 10;
}
void main() {
try {
foo(-5);
}
catch (CS240Exception & e) {
cout << e.GetMessage() << endl;
}
}
The CS240Exception class can be used anywhere in your code where you need to throw an exception.
Placing a descriptive message in an exception object helps to provide additional information
about the error to the code that handles the exception.
Usually, the message is printed out for the benefit of the user.
While the message in the exception object is useful for providing information to the user, it is not
very useful to any code in the program that might want to handle the exception and recover from the
error.
One way to provide more information to the code handling the exception is to create an exception class
for each different type of error that might occur in a program.
When an error occurs, the program throws an exception that is specific to the type of error that has
occurred rather than throwing an object of the generic CS240Exception class.
This allows the code that handles the exception to determine specifically what kind of error has
occurred, hopefully making it possible to recover from the error.
For example, the CS 240 Utilities provide several exceptions classes that are useful for certain types
of errors.
- Class NetworkException is used for network-related exceptions
- Class FileException is used for file-related exceptions
- Class IOException is used for other input/output exceptions
- Class InvalidURLException is used to indicate that a URL is invalid
- Class InvalidArgumentException is used when an invalid parameter is passed to a method
- Class IllegalStateException is used when a method is called at the inappropriate time, or
when an object finds itself in a corrupted state
Each of these error-specific exception classes is a subclass of CS240Exception.
Objects of these types can be initialized with an informative message, and they also inherit
the GetMessage method from CS240Exception for retrieving the message from the exception object.
The following code shows how to use these exception classes.
void bar() {
// code that might throw many different kinds of exceptions goes here
}
void main() {
try {
bar();
}
catch (NetworkException & e) {
// handle network exception
}
catch (FileException & e) {
// handle file exception
}
catch (IOException & e) {
// handle i/o exception
}
catch (InvalidURLException & e) {
// handle invalid URL exception
}
catch (InvalidArgumentException & e) {
// handle invalid argument exception
}
catch (IllegalStateException & e) {
// handle illegal state exception
}
catch (CS240Exception & e) {
// handle unknown exception
}
}
Some of the methods on the CS 240 Utilities classes throw the exception types listed above.
When calling methods that might throw exceptions, you need to write code to handle
the exceptions, or your program will terminate abnormally when exceptions are thrown.
You may also throw these exceptions from methods that you write.
You are encouraged to create new subclasses of CS240Exception that apply to new kinds of errors
that are not covered by the classes listed above.
Consult the code for NetworkException, FileException, etc. for examples of how to properly subclass
CS240Exception.
Of course, any new exception subclasses that you create can have other constructor parameters
and methods in addition to those supported by CS240Exception.
These subclasses can store any information that needs to be passed from the point where an exception
occurs to the point where the exception is handled.
CS240Exception
#include "CS240Exception.h"
The CS240Exception class serves as the base class for all exception types used in the CS 240 Utilities.
CS240Exception may be used directly when an exception needs to be thrown, or it may be subclassed
to create exceptions types that are specific to particular kinds of errors.
Methods
CS240Exception()
The default constructor initializes the exception object with a generic error message.
CS240Exception(const string & msg)
This constructor initializes the exception object with the specified message.
CS240Exception(const CS240Exception & e)
Copy constructor
~CS240Exception()
Destructor
const string & GetMessage()
This method returns the message contained in the exception object.
InvalidArgumentException
#include "CS240Exception.h"
The InvalidArgumentException class is a subclass of CS240Exception.
This exception is thrown when a method receives an invalid parameter value.
Methods
InvalidArgumentException()
The default constructor initializes the exception object with a generic error message.
InvalidArgumentException(const string & msg)
This constructor initializes the exception object with the specified message.
InvalidArgumentException(const InvalidArgumentException & e)
Copy constructor
~InvalidArgumentException()
Destructor
const string & GetMessage()
This method returns the message contained in the exception object.
InvalidURLException
#include "CS240Exception.h"
The InvalidURLException class is a subclass of CS240Exception.
This exception is thrown when an invalid URL is passed as a parameter value.
Methods
InvalidURLException()
The default constructor initializes the exception object with a generic error message.
InvalidURLException(const string & msg)
This constructor initializes the exception object with the specified message.
InvalidURLException(const InvalidURLException & e)
Copy constructor
~InvalidURLException()
Destructor
const string & GetMessage()
This method returns the message contained in the exception object.
IllegalStateException
#include "CS240Exception.h"
The IllegalStateException class is a subclass of CS240Exception.
This exception is thrown when a method is called at an inappropriate time,
or when an object finds itself in a corrupted state.
Methods
IllegalStateException()
The default constructor initializes the exception object with a generic error message.
IllegalStateException(const string & msg)
This constructor initializes the exception object with the specified message.
IllegalStateException(const IllegalStateException & e)
Copy constructor
~IllegalStateException()
Destructor
const string & GetMessage()
This method returns the message contained in the exception object.
IOException
#include "CS240Exception.h"
The IOException class is a subclass of CS240Exception.
This exception is thrown when an input/output error occurs.
Methods
IOException()
The default constructor initializes the exception object with a generic error message.
IOException(const string & msg)
This constructor initializes the exception object with the specified message.
IOException(const IOException & e)
Copy constructor
~IOException()
Destructor
const string & GetMessage()
This method returns the message contained in the exception object.
NetworkException
#include "CS240Exception.h"
The NetworkException class is a subclass of CS240Exception.
This exception is thrown when a network-related error occurs.
Methods
NetworkException()
The default constructor initializes the exception object with a generic error message.
NetworkException(const string & msg)
This constructor initializes the exception object with the specified message.
NetworkException(const NetworkException & e)
Copy constructor
~NetworkException()
Destructor
const string & GetMessage()
This method returns the message contained in the exception object.
FileException
#include "CS240Exception.h"
The FileException class is a subclass of CS240Exception.
This exception is thrown when a file-related error occurs.
Methods
FileException()
The default constructor initializes the exception object with a generic error message.
FileException(const string & msg)
This constructor initializes the exception object with the specified message.
FileException(const FileException & e)
Copy constructor
~FileException()
Destructor
const string & GetMessage()
This method returns the message contained in the exception object.
Ken Rodham