Best Practices while writing Java code

Usually many of us write the java code in a try-catch block as follows

try
{
// some code here
}
catch(Exception e)
{
// log exception here
}

Better way to do it is

try
{
// some code here
}
catch(Exception e)
{
// log exception here
}
catch(Throwable ex)
{
// One must add throwable clause always
// because in case of some error also, you can
// log it and see, what error has occured.
}

If you do not add Throwable clause, and in production, some error occurs, Then nothing will be logged in your logs, you may end up breaking your head, why the hell it is not working.

Make it a habit to use try-catch-Throwable in place of try-catch-Exception

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.