I have a question regarding the exception handler.
I have a structured code for my project which has many packages and classes for different purposes.
In this code, there are many places where I try to catch different types of exceptions.
To mention a few of them are SAXException
, IOException
, NumberFormatException
, ParserConfigurationException
, RuntimeException
and so on.
In all the cases where I catch the exceptions, I only print a stack trace. The moment I come across an exception, I will figure out from the stack trace the function where it occurred and fix it accordingly. I don't do anything else with the exception, neither I intend to do, since the code size is not huge and fairly easy to debug for me.
Now, I am using an external java library which is provided by a third-party developer. This library throws exception for every possible function that I call. I am trying to write a wrapper over this library in order to utilise it. However, I find try/catch blocks everywhere in my code, due to this library.
For example my code looks like this -
Class Wrapper
{
public void method1()
{
....
try
{
...
third party library calls...
...
} catch (Exception e) { e.printStackTrace(); }
}
public void method2()
{
....
try
{
...
third party library calls...
...
} catch (Exception e) { e.printStackTrace(); }
}
// ... and so on... there are 50-100 methods like this.
// some are even one-liners.
}
Given this scenario, should I switch to Global Exception handler as mentioned in this discussion?
Will this avoid write try/catch blocks everywhere in my code?
Also, should I remove the existing try/catch blocks?
Thanks!