8

Let's say you have some method declaring throwing an unchecked exception

EDIT: let's say it's not you who designed this method, but authors of a very respectable framework (Spring, aekhm!), you're just calling it

void someMethod() throws UncheckedException;

My first question is:

  1. Is there any reason other than clarity for declaring that unchecked exception in the throws clause?

Let's say you have another method that's calling someMethod

void someOtherMethod() {
    someMethod()
}

My second question is:

  1. What would be the best practice of deciding whether to declare throwing the UncheckedException in the someOtherMethod?

Just a little background:

Spring framework's exception are based on unchecked exceptions. So for example some methods are throwing (and declaring it in the throws ) DataAccessException. If my code is using these calls, should it or should it not declare throwing these exceptions? And why?

artur
  • 1,710
  • 1
  • 13
  • 28
  • possible duplicate of [Is there an advantage to declaring that a method throws an unchecked exception?](http://stackoverflow.com/questions/24554057/is-there-an-advantage-to-declaring-that-a-method-throws-an-unchecked-exception) – Raedwald Sep 09 '14 at 11:50
  • 1
    @Raedwald Partial duplicate. It doesn't address question two. – Duncan Jones Sep 09 '14 at 11:52

5 Answers5

7

There is no need to declare unchecked exceptions in the throws ... clause of a method. As stated in the Java Language Specification, "It is permitted but not required to mention unchecked exception classes in a throws clause".

It is common practice to list unchecked exceptions in your Javadoc if you anticipate that users of your API might encounter them. A common example of this is to list why an IllegalArgumentException might be thrown.

If you wrap one method with another, apply the Effective Java principle of throwing exceptions suitable to the level of abstraction (Item 61). This should be applied to both checked exceptions and expected unchecked exceptions.

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
  • I understand there's no need to declare unchecked exceptions, but the guys from Spring decided for some reason they will (so I am guessing they have a reason to do this). Many methods of this class `org.springframework.orm.ibatis.SqlMapClientTemplate` declares throwing `org.springframework.dao.DataAccessException` in their `throws` clause. So I suppose my question was more, if my code is calling these methods, should I declare throwing that exception or not and what would be the reasons for doing so (or not doing so) – artur Sep 09 '14 at 11:54
  • @artur I suppose that if Spring has decided to exclusively use unchecked exceptions, perhaps they felt declaring in the `throws` offers some minor benefits to developers who don't have the Javadocs to hand. In such a case, I'd treat them like checked exceptions and catch anything you don't want bubbling up to the next layer. You could consider wrapping them in checked exceptions if you'd like (again remembering to abstract appropriately). If you've not read Item 61, do try to get a copy. – Duncan Jones Sep 09 '14 at 11:57
  • Obviously Spring is violating Item 62 about not declaring Unchecked exceptions in the `throws` clause. I understand Item 61. Treating unchecked exceptions as checked exceptions makes my head spinning. I mean, what's the point of it being unchecked in the first place. – artur Sep 09 '14 at 12:13
  • @artur If you feel you can't do anything about those exceptions, then just let them bubble up. The Spring guys may have opted for unchecked because they think you generally can't do anything about the situation, programmatically. See [why hibernate changed HibernateException to RuntimeException (unchecked)](http://stackoverflow.com/a/4609881) for a similar discussion about Hibernate. – Duncan Jones Sep 09 '14 at 12:16
  • I read that question. I understand (I think) the reasons behind using unchecked exceptions. I just don't understand declaring them in the `throws`. Thanks for your insights though, they are very informative. – artur Sep 09 '14 at 13:31
4

I recommend reading chapter 9 in the great book "Effective Java". You will get all the answers to your questions and you will enjoy a great reading.

Specifically for you questions:

Use the Javadoc @throws tag to document each unchecked exception that a method can throw, but do not use the throws keyword to include unchecked exceptions in the method declaration

shlomi33
  • 1,458
  • 8
  • 9
2

Best practice is to not declare unchecked exceptions at all. You don't see methods like this:

public void foo() throws NullPointerException {...}

You should only use throws for Checked Exceptions. By saying throws you expect clinet to handle his Exception in any way and this is not true for Unchecked Exceptions

EDIT:

There has been some lively discussion in comments so just to clarify: you don't have to declare Unchecked Exceptions which doesn't mean you cannot, although in most cases you shouldn't. I prefer to mention them in javadoc style comments rather than in throws clause. There is plenty other examples where you generally shouldn't do something but in some cases you may need to. In my opinion the bottom line is: no, you shouldn't be listing Unchecked Exceptions in throws clause.

Lucas
  • 3,181
  • 4
  • 26
  • 45
  • Have a look at the Java standard libraries. There are declarations of unchecked exceptions all over the place! – shlomi33 Sep 09 '14 at 11:36
  • 1
    Yes there are also not meaningful variable names all over the place and it's not a good practice either... – Lucas Sep 09 '14 at 11:37
  • It is common practice to also mention many of the unchecked exceptions in the javadoc documentation (for instance `NullPointer` in the official Java API) – Thirler Sep 09 '14 at 11:37
  • 1
    It's a documentation - not the actual code. And also it still doesn't indicate that it's a good practice – Lucas Sep 09 '14 at 11:38
  • 1
    @Thirler : Unchecked exceptions are mentioned in the `@throws` of the docstring, but are they really declared in the sugnature of the method ? – Dici Sep 09 '14 at 11:39
  • @shlomi33 Citation needed! Here is the counter argument: http://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html specificaly this: `Runtime exceptions represent problems that are the result of a programming problem, and as such, the API client code cannot reasonably be expected to recover from them or to handle them in any way.` – Mikkel Løkke Sep 09 '14 at 11:39
  • Most of the times Unchecked Exception indicate programmer's error and you shouldn't try to recover from them (again - not all of them) – Lucas Sep 09 '14 at 11:45
1

To my mind, best practice is to not use throws in the head of the method for unchecked exception, but don't forget to document every exception (checked or not) thrown by your method :

/**
 * Some doc
 * @throws IllegalArgumentException if ...
 */
 public void m() {
     //...
     throw new IllegalArgumentException();
 }
Dici
  • 25,226
  • 7
  • 41
  • 82
1

Documenting your API is a good thing and either declaring a throws or adding @throws to your javadoc is good practice. It just makes things more explicit for the reader, which is never a bad thing. I don't always do this but, generally, you want people to be aware that something might fail. I wouldn't do this for NPEs but things like input validation errors or authorization errors are things you might want to document.

Jilles van Gurp
  • 7,927
  • 4
  • 38
  • 46