I find that sometimes exceptions are useful for building DSLs . I know this sounds strange, but since many languages ββdonβt have Ruby functions (the return keyword is optional because the result of the last processed expression is returned), we use what we can.
For example, I recently tried to create a small JavaScript testing platform, and I wanted infrastructure users to be able to say:
skip(); pending("pending message"); fail("failure message");
instead:
return skip(); return pending("pending message"); return fail("failure message");
These functions would be library functions that throw exceptions, such as a SkipTest exception. The only recommended place to use them is inside test methods. These test methods are always executed in the try / catch block, where each type of exception is processed, and, depending on the exception, the environment takes the appropriate step.
This is an example when I used Exceptions for control flow.
Here is a small example:
$.it("should offer means to explicitly mark a spec as failed", function() { $.fail("this spec must fail"); });
source share