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

This entry was posted in Best Practices, Design Pattern, Java, Server Side Programming and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s