• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Where to wrap exceptions in a stack of private methods.

 
Ranch Hand
Posts: 157
Netbeans IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello:

Let's suppose that I've a public method. This one calls several private methods created to encapsulate and simplify the code. These private methods, in turn, may call other private methods or, in the end, call public methods from other APIs, that may throw their own specific exceptions.

Nowadays, whenever a method from an external API throws exceptions, I encapsulate all them in the private method that contains the call, using a custom exception, and "push" it up in the call stack. This leads me to have a try-catch block in every private method. This is nice because I obtain very "granulated" messages that are readable by humans.

However, in many cases these exceptions are unlikely (or even impossible) to happen, because they only depend on parameters under my control. And, in the case that they may be thrown, my pretty messages don't prevent me from having to check the call stack to find the exact origin of the problem.

So today I was wondering if it wouldn't be more clever and convenient to let all the exceptions "run" up in the call stack, up to the first public method that exists, which will catch and wrap them. This is the only method that it's exposed outside, so the exception message should describe something from the point of view of the external caller, not about internals failures, Right? What do you opine?

Thank you.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by wrapping exceptions? Do you mean Exception chaining?
Are those checked Exceptions or unchecked? Would those private methods have a chance to repeat the action if an Exception occurred? Would they be able to terminate the application cleanly? Consider that sort of question, because I don’t think we can give a hard and fast rule about where to catch such Exceptions.
 
Avor Nadal
Ranch Hand
Posts: 157
Netbeans IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell Ritchie: I'm sorry for not being more concrete.

By wrapping exceptions I mean assigning them as the cause of another one, to give the error a more specific context. I guess the term "chaining" is the correct one. Something similar to this pseudo-code:



About the question if they're checked or unchecked exceptions, in my personal case they all are checked exceptions. For example, the IOException is very common. Because it doesn't tell anything about what is being read, I always chain it with a custom exception that adds that additional information.
 
Avor Nadal
Ranch Hand
Posts: 157
Netbeans IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Definitively, I'm getting rid of the idea of wrapping/chaining the exceptions always in the highest method of the private method call stack.

I've realized that this makes the code less readable and also breaks the idea of encapsulating code inside of smaller and more specific methods. For example, if the mission of a private method is simply reading a file, it has no sense that methods in higher positions of the stack deal with such generic exceptions. Better to throw more specific exceptions even if they're going to be kept in this private scope.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that is what you call exception chaining. It is difficult, as I said before, to advise you where to catch such exceptions. If method foo() can handle that Exception, then let foo() handle it. Otherwise write a throws clause and let another method handle the exception. It might be worth revising the Java Tutorials about exceptions.
 
Avor Nadal
Ranch Hand
Posts: 157
Netbeans IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:It is difficult, as I said before, to advise you where to catch such exceptions.



Campbell Ritchie: I'm sorry for not mentioning it before, but you were right, of course. Thank you ;) .

I thought that it was easier to set some kind of rule to apply everywhere. But definitively handling exceptions depends on the context a lot. Checking my own code I've seen places where it's better to chain exceptions in the lowest private method, whereas in other places it makes more sense to do it one or two methods up.

I guess that a good clue to chain exceptions in private methods would be to do it only in those places where you can add some kind of valuable information, relative to that context. If you can't, let the exception be handled upper.
 
reply
    Bookmark Topic Watch Topic
  • New Topic