2

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!

Community
  • 1
  • 1
Raj
  • 3,300
  • 8
  • 39
  • 67
  • Use pokemon error handler - catch them all! One try / catch over all code. – Bogdan Burym Feb 07 '13 at 16:11
  • 3
    You have to handle checked exceptions, no getting around that. And swallowing exceptions by printing their stack trace is an anti-pattern, you should really be handling them or re-throwing for a higher level class to handle. – Perception Feb 07 '13 at 16:12

2 Answers2

2

If you don't mind your program exiting after any exception, you can probably use a global exception handler. You may have to put throws Exception (or a more specific exception class) at a whole bunch of places (to prevent unhandled-exception compile-time errors), which is very far from ideal (and could end up with less "pretty" code than catching exception where they are thrown).

If, on the other hand, you want your program to recover from errors (which is generally wanted in production-level code), you'll need appropriately placed try-catch statements (which could involve having try-catch statements everywhere in your code).

As example, if you get an IOException, you may want to retry, or if you get a NumberFormatException, you may want to notify the user that the input was invalid and let him/her try again.

You should not just remove try-catch statement, you should look at what they do, whether this behaviour is what you want and whether it will be reproducible with alternative solutions (e.g. a global try-catch statement).

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
  • if it is the case that I may not be able to proceed further whenever an exception occurs? I build a compilation and simulation tool which has input through XML and output in form of file. There is no interaction with user whatsoever. So if there is an error in input file, the compilation cannot proceed further. – Raj Feb 07 '13 at 16:21
  • 1
    @Raj Then you may have to put a bunch of `throws` statements in your functions, or at least catch the minimal amount of exceptions (for whichever exceptions cause unhandled-exception compile-time errors). – Bernhard Barker Feb 07 '13 at 17:00
2

If you do not want your client code to handle checked exceptions, you can do something like following in your wrapper.

public void method1() {
     try {
        //3rd party code here....
     }
     catch(RuntimeException e){
         throw e;
     }
     catch(Exception e){
       throw new RuntimeException(e.getMessage(),e);
     }
}

Notice, this avoids swallowing the exceptions thrown by 3rd party library and does not force clients to handle Checked exceptions like IOException, SQLException etc.

Bimalesh Jha
  • 1,464
  • 9
  • 17
  • thanks for your answer. however my problem is that I implement lets say 200 methods which are wrapper over this third party library. each of the method is having try/catch blocks. I want to know that if there is a way to avoid it. – Raj Feb 07 '13 at 16:44
  • @Raj are they part of a single class? You can think of invoking them using `Reflection`. Some IDEs like Eclipse provide automated code generation for method delegation. You can use that too. – Bimalesh Jha Feb 07 '13 at 17:09