October 19, 2014

Exception Handling Tips For Developers

What is Exception?

Exception is a bad behavior of software can prevent doing correct jobs as we expected.

 

Exceptions Types

There are two major types of exceptions:

  1. Predictable Exceptions
         Example: Web-service exceptions
         Responsible to manage: Developer
     
  2. Unpredictable Exceptions
        Example: Database Failure
        Responsible to manage: Total Solution(Software Architect)


When exceptions are occuring

In three main places exceptions can occur:

  1. One internal resource is out of order
    Example: Database Failure or mismatch.(Unpredictable)
     
  2. One external resource is out of order
    Example: a web service or a file on hard-drive is not available. (Predictable)
     
  3. A business rule throw an exception
    like: object reference is null. (Predictable / Unpredictable)

 

How We Handle Exceptions

There are two general ways to handle exceptions:

  1. General Exception Handling infrastructure
    To catch all Unpredictable exceptions, and after notifying administrator, shows a well-known page for end users.
     
  2. Development Eception Handling blocks
    Writing Exception-Handling blocks inside the code to manage Predictable exceptions by developers.

 

Handle Unpredictable Exceptions

  • ‚ÄčHandling Unpredictable Exceptions is a part of Software Architect Jobs.
     
  • Unpredictable Exceptions Handling is a cross-cutting concern. It means, by using AOP(Aspect Oriented Programming) a general solution will be designed to handle all of them, rather than putting exception handler blocks everywhere inside the code.

 

Handle Predictable Exceptions

As a developer how are you managing exceptions?

  1. External Resources: 
    When you are using external resources like: web-services or a file, you have to handle all exceptions.
    You must implement exception handling in the code.(do not forget)
  2. Business Rules:
    When you are implementing business rules
  • Certainly validate all required objects to prevent accessing null object exceptions. 
  • If some predictable and acceptable exceptions are happening, handle all of them and try to return a proper message.

Important Notice: Normally, do not put exception block everywhere. This type of coding will dirty up your project and will cause a lot of business rule bugs. If you can not predict an exception, just follow above rules, otherwise let general solution of exception handling to catch up exceptions. 

 

Bad Practices In Exception Handling

  • Inside your code, you can see exception handling blocks everywhere.
     
  • Your business functions are talking each others using exceptions, not messages. (invalid parameters should be reported by a text message not an exception)
     
  • End users do not visit a well-designed error-page when unhandled exceptions are occurring.
     
  • Your software does not throw any exception but there are a lot of bugs which are coming from business rules.