Exception Handling

Exception Handling

Exceptions in software development are anomalous, unexpected conditions into which a program might enter if an unsupported operation gets executed. Exceptions break the flow of control and can either trigger a pre-configured handler or bubble up the stack.

The occurrence of an exception is generally known as a "throw" event. The handling of an exception is similarly known as a "catch" event.

Based on how exceptions are thrown and subsequently handled in a language, they fall under two main types:

  • Exceptions as errors: In these languages, exceptions describe situations where the program has entered an abnormal, unpredictable state.
  • Exceptions as the flow of control: In these languages, exceptions are constructs used to break the normal flow of a program and jump to the exception handler.

Structured programming languages tend to use exceptions as errors. Using them for the flow of control can break the single entry-single exit paradigm of these languages and create multiple pathways within the program.

What happens in the absence of exception handling?

If an exception is thrown and not caught, it usually bubbles upward through the stack. Bubbling means that the exception thrown in a function moves upwards through the call stack unless a function handles it. Suppose the exception remains uncaught until the application level. In that case, the exception passes onto the runtime whose default behavior is to terminate the application with the error message and the stack trace showing the origin and flow of the exception.

Write clean and secure code with DeepSource

Powerful static analysis that takes 5 minutes to set up and helps you fix code health and security problems on every pull request.