exceptions.cpp
Go to the documentation of this file.
00001 
00013 /*****************************************************************************
00014 ** Includes
00015 *****************************************************************************/
00016 
00017 #include <cstdio> // fopen
00018 #include <iostream>
00019 #include <ecl/config/ecl.hpp>
00020 #include "../../include/ecl/exceptions/exception.hpp"
00021 #include "../../include/ecl/exceptions/data_exception.hpp"
00022 #include "../../include/ecl/exceptions/macros.hpp"
00023 #include "../../include/ecl/exceptions/standard_exception.hpp"
00024 
00025 /*****************************************************************************
00026 ** Disable check
00027 *****************************************************************************/
00028 
00029 #include <ecl/config/ecl.hpp>
00030 #ifndef ECL_DISABLE_EXCEPTIONS
00031 
00032 /*****************************************************************************
00033 ** Using
00034 *****************************************************************************/
00035 
00036 using namespace ecl;
00037 
00038 /*****************************************************************************
00039 ** Functions
00040 *****************************************************************************/
00041 
00042 void f(int i) ecl_debug_throw_decl(StandardException)
00043 {
00044     if ( i > 5 ) {
00045         ecl_debug_throw(StandardException(LOC,InvalidInputError,"Debug throw test failed, input integer exceeded."));
00046     } else {
00047 //        cout << "Valid integer supplied." << endl;
00048     }
00049 }
00050 
00051 /*****************************************************************************
00052 ** Main
00053 *****************************************************************************/
00054 
00055 int main(int argc, char **argv) {
00056 
00057     std::cout << std::endl;
00058     std::cout << "***********************************************************" << std::endl;
00059     std::cout << "                  Standard Exceptions" << std::endl;
00060     std::cout << "***********************************************************" << std::endl;
00061     std::cout << std::endl;
00062 
00063     try {
00064         throw StandardException(LOC, NoError);
00065     } catch ( StandardException &e ) {
00066         std::cout << e.what() << std::endl;
00067     }
00068     try {
00069         throw StandardException(LOC, UnknownError);
00070     } catch ( StandardException &e ) {
00071         std::cout << e.what() << std::endl;
00072     }
00073     try {
00074         throw StandardException(LOC, OutOfRangeError);
00075     } catch ( StandardException &e ) {
00076         std::cout << e.what() << std::endl;
00077     }
00078     try {
00079         throw StandardException(LOC, ConstructorError);
00080     } catch ( StandardException &e ) {
00081         std::cout << e.what() << std::endl;
00082     }
00083     try {
00084         throw StandardException(LOC, DestructorError);
00085     } catch ( StandardException &e ) {
00086         std::cout << e.what() << std::endl;
00087     }
00088     try {
00089         throw StandardException(LOC, ConversionError);
00090     } catch ( StandardException &e ) {
00091         std::cout << e.what() << std::endl;
00092     }
00093     try {
00094         throw StandardException(LOC, OpenError);
00095     } catch ( StandardException &e ) {
00096         std::cout << e.what() << std::endl;
00097     }
00098     try {
00099         throw StandardException(LOC, CloseError);
00100     } catch ( StandardException &e ) {
00101         std::cout << e.what() << std::endl;
00102     }
00103     try {
00104         throw StandardException(LOC, InvalidArgError);
00105     } catch ( StandardException &e ) {
00106         std::cout << e.what() << std::endl;
00107     }
00108     try {
00109         throw StandardException(LOC, ConfigurationError);
00110     } catch ( StandardException &e ) {
00111         std::cout << e.what() << std::endl;
00112     }
00113     try {
00114         throw StandardException(LOC, ConnectionError);
00115     } catch ( StandardException &e ) {
00116         std::cout << e.what() << std::endl;
00117     }
00118     try {
00119         throw StandardException(LOC, ReadError);
00120     } catch ( StandardException &e ) {
00121         std::cout << e.what() << std::endl;
00122     }
00123     try {
00124         throw StandardException(LOC, WriteError);
00125     } catch ( StandardException &e ) {
00126         std::cout << e.what() << std::endl;
00127     }
00128     try {
00129         throw StandardException(LOC, NotInitialisedError);
00130     } catch ( StandardException &e ) {
00131         std::cout << e.what() << std::endl;
00132     }
00133     try {
00134         throw StandardException(LOC, PermissionsError);
00135     } catch ( StandardException &e ) {
00136         std::cout << e.what() << std::endl;
00137     }
00138     try {
00139         throw StandardException(LOC, MemoryError);
00140     } catch ( StandardException &e ) {
00141         std::cout << e.what() << std::endl;
00142     }
00143     try {
00144         throw StandardException(LOC, UsageError);
00145     } catch ( StandardException &e ) {
00146         std::cout << e.what() << std::endl;
00147     }
00148     try {
00149         throw StandardException(LOC, RaiiError);
00150     } catch ( StandardException &e ) {
00151         std::cout << e.what() << std::endl;
00152     }
00153     try {
00154         throw StandardException(LOC, ArgNotSupportedError);
00155     } catch ( StandardException &e ) {
00156         std::cout << e.what() << std::endl;
00157     }
00158     try {
00159         throw StandardException(LOC, NotSupportedError);
00160     } catch ( StandardException &e ) {
00161         std::cout << e.what() << std::endl;
00162     }
00163     try {
00164         throw StandardException(LOC, BusyError);
00165     } catch ( StandardException &e ) {
00166         std::cout << e.what() << std::endl;
00167     }
00168     try {
00169         throw StandardException(LOC, OutOfResourcesError);
00170     } catch ( StandardException &e ) {
00171         std::cout << e.what() << std::endl;
00172     }
00173     try {
00174         throw StandardException(LOC, InterruptedError);
00175     } catch ( StandardException &e ) {
00176         std::cout << e.what() << std::endl;
00177     }
00178 
00179     std::cout << std::endl;
00180     std::cout << "***********************************************************" << std::endl;
00181     std::cout << "       Standard Exceptions - Custom Message" << std::endl;
00182     std::cout << "***********************************************************" << std::endl;
00183     std::cout << std::endl;
00184 
00185     try {
00186         throw StandardException(LOC, OutOfRangeError,"Custom detailed message.");
00187     } catch ( StandardException &e ) {
00188         std::cout << e.what() << std::endl;
00189     }
00190 
00191     std::cout << std::endl;
00192     std::cout << "***********************************************************" << std::endl;
00193     std::cout << "       Standard Exceptions - Custom Message" << std::endl;
00194     std::cout << "***********************************************************" << std::endl;
00195     std::cout << std::endl;
00196     try {
00197         throw StandardException(LOC, OutOfRangeError,"Custom detailed message.");
00198     } catch ( StandardException &e ) {
00199         std::cout << e.what() << std::endl;
00200     }
00201 
00202     std::cout << std::endl;
00203     std::cout << "***********************************************************" << std::endl;
00204     std::cout << "         Assert Exception Macro" << std::endl;
00205     std::cout << "***********************************************************" << std::endl;
00206     std::cout << std::endl;
00207 
00208     try {
00209         ecl_assert_throw(2<1, StandardException(LOC, InvalidInputError,"Two is not smaller than one!"));
00210     } catch ( StandardException &e ) {
00211         std::cout << e.what() << std::endl;
00212     }
00213 
00214     std::cout << std::endl;
00215     std::cout << "***********************************************************" << std::endl;
00216     std::cout << "             Debug Throw Macro" << std::endl;
00217     std::cout << "***********************************************************" << std::endl;
00218     std::cout << std::endl;
00219 
00220     try {
00221         f(7);
00222     } catch ( StandardException &e ) {
00223         std::cout << e.what() << std::endl;
00224     }
00225 
00226     std::cout << std::endl;
00227     std::cout << "***********************************************************" << std::endl;
00228     std::cout << "                 Rethrowing" << std::endl;
00229     std::cout << "***********************************************************" << std::endl;
00230     std::cout << std::endl;
00231 
00232     try {
00233         throw StandardException(LOC, ConfigurationError,"Starting spot for a rethrow.");
00234     } catch ( StandardException &e1 ) {
00235         try {
00236             throw StandardException(LOC, e1);
00237         } catch ( StandardException &e2 ) {
00238             std::cout << e2.what() << std::endl;
00239         }
00240     }
00241 
00242     std::cout << std::endl;
00243     std::cout << "***********************************************************" << std::endl;
00244     std::cout << "                    Try Catch" << std::endl;
00245     std::cout << "***********************************************************" << std::endl;
00246     std::cout << std::endl;
00247 
00248 
00249     std::cout << "An ecl try-catch block." << std::endl;
00250     std::cout << std::endl;
00251     StandardException e_original(LOC,UnknownError);
00252         ecl_try {
00253                 std::cout << "Try" << std::endl;
00254                 ecl_throw(StandardException(LOC,e_original));
00255         } ecl_catch( StandardException &e ) {
00256                 std::cout << "Catch" << std::endl;
00257                 std::cout << e.what() << std::endl;
00258         }
00259     std::cout << "An debug try-catch block." << std::endl;
00260     std::cout << std::endl;
00261         ecl_debug_try {
00262                 std::cout << "Try" << std::endl;
00263                 ecl_debug_throw(StandardException(LOC,e_original));
00264         } ecl_debug_catch( StandardException &e ) {
00265                 std::cout << "Catch" << std::endl;
00266                 // std::cout << e.what() << std::endl; // This fails in release
00267         }
00268 
00269 
00270     std::cout << std::endl;
00271     std::cout << "***********************************************************" << std::endl;
00272     std::cout << "                 Data Exception" << std::endl;
00273     std::cout << "***********************************************************" << std::endl;
00274     std::cout << std::endl;
00275 
00276     try {
00277         throw DataException<int>(LOC, ConfigurationError,"Throwing a data exception with an int.", 7);
00278     } catch ( DataException<int> &e ) {
00279         std::cout << "Data: " << e.data() << std::endl;
00280         std::cout << e.what() << std::endl;
00281     }
00282 
00283     std::cout << std::endl;
00284     std::cout << "***********************************************************" << std::endl;
00285     std::cout << "                      Passed" << std::endl;
00286     std::cout << "***********************************************************" << std::endl;
00287     std::cout << std::endl;
00288 
00289     return 0;
00290 }
00291 
00292 #else
00293 /*****************************************************************************
00294 ** Main
00295 *****************************************************************************/
00296 
00297 int main(int argc, char **argv) {
00298         std::cout << "Ecl exceptions are unavailable (ecl was compiled with ECL_DISABLE_EXCEPTIONS)." << std::endl;
00299 
00300         return 0;
00301 }
00302 
00303 #endif /* ECL_DISABLE_EXCEPTIONS */


ecl_exceptions
Author(s): Daniel Stonier
autogenerated on Wed Aug 26 2015 11:27:05