• Post Reply Bookmark Topic Watch Topic
  • New Topic

a little bit revisitting on exception handling  RSS feed

 
David Spades
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

So, consider this code:



so with this code, it doesn't matter anymore how many custom exceptions third party lib might throw, and I assume that stack trace will also be preserved even when using base class exception (for logging purposes), so bug hunting is not an issue with this approach, right? so what are the cons of this approach?
the major concern is custom exceptions.

thanks
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What the construct means is, “I don't know whether any of the implementations of this method will need to declare an Exception.” That means you can have any implementing method handle its own Exceptions or throw any sort of Exception. Or not have any Exceptions at all.

Yes, all Exceptions thrown will behave completely normally. There will be peculiar behaviour for subclasses of Exception.
 
David Spades
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There will be peculiar behaviour for subclasses of Exception.


could you elaborate? thanks
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a mispritn. Sorry. It should say no peculiar behaviour.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The disadvantage of this approach is that the caller of such method will have to catch Exception (or pass the buck up the stack, but it will have to be handled somewhere eventually). I generally don't like catching Exception, as it matches RuntimeException and it's children as well, and you might end up catching (and perhaps handling somehow, probably in a completely inappropriate way) occurrences of NullPointerException and the like.

If the intent of this interface is to wrap a third-party library, I'd suggest creating a specific (checked or unchecked - depending on your taste) exception, catching all exceptions from that library in the implementation of doSomething() and wrapping them in that specific exception. It's better not to expose the details of the implementation of the third-party library to the users of the interface, especially if the purpose of the interface is to make the library interchangeable. (Just remember to pass the original exception to the constructor of the wrapping exception, otherwise you won't get a complete stack trace.)
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you do not have to catch plain simple Exception because the implementing methods need not declare that they throw Exception. They may declare any of its subtypes or nothing at all.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But surely it will complain if you DON'T handle the exception?

The interface declares it throws a checked exception.
You either need to handle it or declare that you can throw it.

The implementation may or may not actually throw an Exception at all.
But anyone dealing with this interface will have to anticipate one regardless.

Personally, I think it smells.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The interface declares an Exception.
Implementing methods can declare any subtypes or no Exceptions at all.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly. so in essence you are giving the implementor a free license to do whatever they want.
The cost is borne by the person using the interface.

If I want to call myInterface.doSomething then I have to catch an exception. Right?

 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:The interface declares an Exception.
Implementing methods can declare any subtypes or no Exceptions at all.


And since we should be coding against interfaces, then the calling code would have to handle Exception.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:If I want to call myInterface.doSomething then I have to catch an exception. Right?

Yes, you have to.

The implementing method can declare to throw an Exception, any subtype(s) of Exception, or nothing at all. But if the method is called through the interface, the caller must handle Exception, since it can't be known which implementation will be used and which exceptions it declares to throw.

Of course, if someone called the implementing method directly (that is, MyImplementation.doSomething() instead of MyInterface.doSomething()), they'd have to handle only those (checked) exceptions declared by the implementation. But if someone wanted to do that, why bother declaring the interface at all?
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compiles and runs nicely. Tried it with an anonymous class and it wouldn't compile because of unhandled Exceptions. I couldn't remember how to run it as a λ.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martin Vajsar wrote: . . . if someone called the implementing method directly (that is, MyImplementation.doSomething() instead of MyInterface.doSomething()), they'd have to handle only those (checked) exceptions declared by the implementation. . . .
That is why it worked when I created that class. Had I declared it as StringChanger rather than StringDoubler it wouldn't have compiled.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tried it with an anonymous class and it wouldn't compile because of unhandled Exceptions. I couldn't remember how to run it as a λ.

Hm, that would be interesting to see. It's true that the compiler can know that the anonymous implementation won't throw the exception, so it could work.

I haven't understood that the OP's question deals with calling the method directly, not via the interface. If it was the case, I wouldn't declare the interface at all, but I've already mentioned this.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something likeWon't print things backwards, because the compiler complains about unhandled Exceptions.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!