Skip to content

Error Handling

The exception handling in java is one of the powerful mechanism to handle the run time errors so that normal flow of the application can be maintained.

  • Use Exceptions Rather Than Return Codes
  • Write Your Try-Catch-Finally Statement First
  • Use Unchecked Exceptions
  • Provide Context with Exceptions

    Create informative error messages and pass them along with your exceptions. Mention the operation that failed and the type of failure.

    If you are logging in your application, pass along enough information to know the reason.

    Define Exception Classes in Terms of a Caller’s Needs

    Instead of writing more numbers of catches ,wrapping those exceptions in one exception.

    Define the Normal Flow

    First separate Business Logic and Error Handling logic. And put that error handling logic in bottom.

    Do not write unnecessary User defined exception.

    Don’t Return Null

    Do not return Null, instead of returning null return Special case object.

    Don’t Pass Null