Coding by exception is an accidental complexity in a software system in which the program handles specific errors that arise with unique exceptions. When an issue arises in a software system, an error is raised tracing the issue back to where it was caught and then where that problem came from.

Exceptions can be used to handle the error while the program is running and avoid crashing the system. Exceptions should be generalised and cover numerous errors that arise. Using these exceptions to handle specific errors that arise to continue the program is called coding by exception.

This anti-pattern can quickly degrade software in performance and maintainability. Executing code even after the exception is raised resembles the goto method in many software languages, which is also considered poor practice.

There’s also the use of exceptions for control flow. This is language dependent but particularly common in Ruby and Python and generally considered an anti-pattern as the code that handles the error can be greatly removed from the code that causes the error. It is usually much slower than conventional code as each scope needs to be checked to see if the error is handled there. There’s also the problem that the error caught may not be the one you intended to handle and that this can silence legitimate errors.

Below are the some syntactical representation of raising exception and exception handling in Ruby, we can use while coding by exception.

# block where exception raise
# block where exception rescue

In above Syntax, the code in which an exception is raised, is enclosed between the begin/end block, so you can use a rescue clause to handle this type of exception.

raise ExceptionType, "Error Message" condition

In above syntax, the first argument is used to create an exception and then set the message in the second argument. You can also set a condition statement to raise an exception.