CS240Exception.h

Go to the documentation of this file.
00001 #ifndef CS240_EXCEPTION_H
00002 #define CS240_EXCEPTION_H
00003 
00004 //!@defgroup exception CS240 Exceptions
00005 //!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 std::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.
00006 //!@code
00007 //!int foo(int x)
00008 //!{
00009 //! if (x < 0)
00010 //! {
00011 //!     throw CS240Exception("negative numbers are illegal");
00012 //! }
00013 //!
00014 //! return x * 10;
00015 //!}
00016 //!
00017 //!void main()
00018 //!{
00019 //! try
00020 //! {
00021 //!     foo(-5);
00022 //! }
00023 //! catch (CS240Exception & e)
00024 //! {
00025 //!     cout << e.GetMessage() << endl;
00026 //! }
00027 //!}
00028 //!@endcode
00029 //! 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.
00030 //!
00031 //! 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.
00032 //!
00033 //!@li Class NetworkException is used for network-related exceptions
00034 //!@li Class FileException is used for file-related exceptions
00035 //!@li Class IOException is used for other input/output exceptions
00036 //!@li Class InvalidURLException is used to indicate that a URL is invalid
00037 //!@li Class InvalidArgumentException is used when an invalid parameter is passed to a method
00038 //!@li Class IllegalStateException is used when a method is called at the inappropriate time, or when an object finds itself in a corrupted state 
00039 //!
00040 //! 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.
00041 //!@code
00042 //!void bar()
00043 //!{
00044 //! // code that might throw many different kinds of exceptions goes here 
00045 //!}
00046 //!
00047 //!void main()
00048 //!{
00049 //! try
00050 //! {
00051 //!     bar();
00052 //! }
00053 //! catch (NetworkException & e)
00054 //! {
00055 //!     // handle network exception
00056 //! }
00057 //! catch (FileException & e)
00058 //! {
00059 //!     // handle file exception
00060 //! }
00061 //! catch (IOException & e)
00062 //! {
00063 //!     // handle i/o exception
00064 //! }
00065 //! catch (InvalidURLException & e)
00066 //! {
00067 //!     // handle invalid URL exception
00068 //! }
00069 //! catch (InvalidArgumentException & e)
00070 //! {
00071 //!     // handle invalid argument exception
00072 //! }
00073 //! catch (IllegalStateException & e)
00074 //! {
00075 //!     // handle illegal state exception
00076 //! }
00077 //! catch (CS240Exception & e)
00078 //! {
00079 //!     // handle unknown exception
00080 //! }
00081 //!}
00082 //!@endcode
00083 //! 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.
00084 
00085 #include <string>
00086 
00087 //!@ingroup exception
00088 class CS240Exception
00089 {
00090 public:
00091     CS240Exception() : message("Unknown Error")
00092     {
00093     }
00094 
00095     CS240Exception(const std::string & msg) : message(msg)
00096     {
00097     }
00098 
00099     CS240Exception(const CS240Exception & e) : message(e.message)
00100     {
00101     }
00102 
00103     virtual ~CS240Exception()
00104     {
00105     }
00106 
00107     const std::string & GetMessage()
00108     {
00109         return message;
00110     }
00111 
00112 protected:
00113     std::string message;
00114 };
00115 
00116 
00117 //!@ingroup exception
00118 class InvalidArgumentException : public CS240Exception
00119 {
00120 public:
00121     InvalidArgumentException() : CS240Exception("Invalid Argument")
00122     {
00123         return;
00124     }
00125 
00126     InvalidArgumentException(const std::string & msg) : CS240Exception(std::string("Invalid Argument: ") + msg)
00127     {
00128         return;
00129     }
00130 };
00131 
00132 
00133 //!@ingroup exception
00134 class InvalidURLException : public CS240Exception
00135 {
00136 public:
00137     InvalidURLException() : CS240Exception("Invalid URL")
00138     {
00139     }
00140 
00141     InvalidURLException(const std::string & msg) : CS240Exception(std::string("Invalid URL: ") + msg)
00142     {
00143     }
00144 };
00145 
00146 
00147 //!@ingroup exception
00148 class IllegalStateException : public CS240Exception
00149 {
00150 public:
00151     IllegalStateException() : CS240Exception("Illegal State")
00152     {
00153     }
00154 
00155     IllegalStateException(const std::string & msg) : CS240Exception(std::string("Illegal State: ") + msg)
00156     {
00157     }
00158 };
00159 
00160 
00161 //!@ingroup exception
00162 class IOException : public CS240Exception
00163 {
00164 public:
00165     IOException() : CS240Exception("I/O Error")
00166     {
00167     }
00168 
00169     IOException(const std::string & msg) : CS240Exception(std::string("I/O Error: ") + msg)
00170     {
00171     }
00172 };
00173 
00174 
00175 //!@ingroup exception
00176 class NetworkException : public CS240Exception
00177 {
00178 public:
00179     NetworkException() : CS240Exception("Network Error")
00180     {
00181     }
00182 
00183     NetworkException(const std::string & msg) : CS240Exception(std::string("Network Error: ") + msg)
00184     {
00185     }
00186 };
00187 
00188 
00189 //!@ingroup exception
00190 class FileException : public CS240Exception
00191 {
00192 public:
00193     FileException() : CS240Exception("File Error")
00194     {
00195     }
00196 
00197     FileException(const std::string & msg) : CS240Exception(std::string("File Error: ") + msg)
00198     {
00199     }
00200 };
00201 
00202 
00203 #endif

Generated on Wed Jul 7 16:30:27 2010 for CS240Utils by  doxygen 1.5.8