I've been searching on google for best practices regarding exception handling, however I did not have much success besides using try-catch.

My framework is set with a EventFiringWebDriverwhich, according to Selenium documentation has a ExceptionThrownevent.

I was thinking in implementing it so we can quickly debug the method that threw the exception, as some colleagues are not programmers and don't know a lot about coding.

The other idea I had is adding try-catchto each method but this seems like a really tedious and worthless endeavour as I would've to keep adding try-catchs to each new method.

So my question is, do you have any best practices regarding exception handling?

  • 1
    Which language are you using? May 2, 2018 at 7:09
  • @VishalAggarwal I am using C#
    – elgato
    May 3, 2018 at 9:01

1 Answer 1


Here are few best practices regarding exception handling in general:

  • Close/Clean Up Resources in a Finally Block (Not at the end of the try block).
  • Use specific exceptions over generic one so that person maintaining the code will have an easier time figuring out issues.
  • Mention the exception detail in the function header comment with the situation it may occur.
  • Provide descriptive message while throwing Exceptions
  • Catch the Most Specific Exception First.
  • Don't Catch Throwable.
  • Don't Ignore Exceptions.
  • Don't Log and Throw.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.