//.............................................................................. // // This file is part of the Jancy toolkit. // // Jancy is distributed under the MIT license. // For details see accompanying license.txt file, // the public copy of which is also available at: // http://tibbo.com/downloads/archive/jancy/license.txt // //.............................................................................. // Jancy provides syntactic sugar over good old C-style error code check model. // It looks just like throwing and catching exceptions and frees the developer // from the routine of constantly checking for error codes. // This model allows the developer to choose between using error code checks or // exception semantics when calling the very same function depending on what is // more appropriate or convenient in each particular case! // It is also extremely easy to support from the host C/C++ application. //.............................................................................. // sample error code enumeration enum Error { Success = 0, TooHigh = -2, TooLow = -3, } //.............................................................................. // A function marked by the 'errorcode' modifier will have its return value // interpreted as an error code. Intuitive error conditions are assumed: // 'false' for bools, negative for signed integers, '-1' for unsigned integers. // Other types will be cast to 'bool' for error condition checking. int errorcode foo (int a) { printf ("foo (%d)\n", a); if (a > 100) { printf ("argument too high\n"); return Error.TooHigh; } if (a < -100) { printf ("argument too low\n"); return Error.TooLow; } // everything is OK, do the actual work... return Error.Success; } // if the return types of the errorcode functions match, the error code is // automatically propagated down the call stack int errorcode bar ( int a, int b ) { printf ("bar (%d, %d)\n", a, b); foo (a); foo (b); return Error.Success; } //.............................................................................. // entry point int main () { printf ("main ()\n"); // use 'try' operator to handle the error code manually... int error = try bar (1, -150); if (error < 0) { printf ("bar returned error %d\n", error); } bar (2, 3); bar (5, 150); return 0; // ...or use the 'catch' label to handle an error in the exception-style // manner catch: printf ("some of the throwing functions above returned an error\n"); return -1; } //..............................................................................