//.............................................................................. // // 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 // //.............................................................................. // The 'finally' clause is traditionally associated only with exception // handling. In Jancy it has a wider application domain. After all, // finalization is not only needed when errors occur, right? // Supposing the developer wants to create a block of finalization code to be // executed upon exiting a certain scope no matter what exit path is taken at // runtime. Why force him to create a 'try' block just for that (as Java would // force him to do)? // If you ever asked yourself this question then we have good news: in Jancy // the 'finally' block can be added to any given scope. //.............................................................................. size_t foo (char const* s) { printf ("foo (%s)\n", s); size_t count = dynamic sizeof (s); for (size_t i = 0; i < count; i++) { int c = s [i]; if (!c) break; if (c == '\t') { printf ("TAB found at %d\n", i); return i; } } printf ("TAB not found\n"); return -1; // nothing to do with exceptions here, just a 'finally' block finally: printf ("foo () finalization\n"); } bool errorcode bar (int a) { printf ("bar (%d)\n", a); if (a < 0) { std.setErrno (22); // EINVAL return false; } return true; } //.............................................................................. // entry point int main () { printf ("main ()\n"); // traditional usage of 'finally' try { foo ("abc"); foo ("def\tghi"); bar (10); bar (-1); printf ("not supposed to get here\n"); catch: printf ("error (%s)\n", std.getLastError ().m_description); finally: printf ("try-block finalization\n"); } return 0; } //..............................................................................